package com.smartstate.ems.service.impl;


import ch.qos.logback.core.joran.util.beans.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.extra.expression.ExpressionUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.smartstate.ems.dto.EnergyDTO;
import com.smartstate.ems.dto.PageDTO;
import com.smartstate.ems.entity.HalfHourPowerMeterSummaryEntity;
import com.smartstate.ems.entity.ObjMoudel;
import com.smartstate.ems.exception.ErrorEnums;
import com.smartstate.ems.exception.MyRException;
import com.smartstate.ems.mapper.ObjMoudelMapper;
import com.smartstate.ems.mapper.PowerMeterSummaryMapper;
import com.smartstate.ems.service.ObjMoudelService;
import com.smartstate.ems.service.PowerMeterSummaryService;
import com.smartstate.ems.util.MyExpressionUtil;
import com.smartstate.ems.util.PowerMeterSummaryUtils;
import com.smartstate.ems.vo.HalfHourPowerMeterSummaryEntityVo;
import com.smartstate.ems.vo.PowerVo;
import org.springframework.beans.BeanUtils;
import org.springframework.security.core.parameters.P;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.time.temporal.ChronoUnit;
import java.util.*;
import java.util.function.Function;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * <p>能源分析</p>
 * <p>@Author 陈永杰</p>
 * <p>@Date: 2022/9/1</p>
 * <p>@Time: 15:48</p>
 * <p>@version 1.0 <p>
 */
@Service
public class PowerMeterSummaryServiceImpl extends ServiceImpl<PowerMeterSummaryMapper, HalfHourPowerMeterSummaryEntity> implements PowerMeterSummaryService {

    @Resource
    private ObjMoudelService objMoudelService;

    @Resource
    private PowerMeterSummaryMapper powerMeterSummaryMapper;

    private Set<String> deviceIdSet;

    @Override
    public Page<HalfHourPowerMeterSummaryEntity> queryAllPageByDTO(PageDTO pageDTO) {
        return null;
    }

    public List<PowerVo> page(EnergyDTO energyDTO) {
        List<Map<String, String>> stringObjMoudelMap = objMoudelService.selectMetering(energyDTO);
        List<PowerVo> listTree = new ArrayList<>();
        List<PowerVo> listPower = new ArrayList<>();
        HashSet<String> hashSet = new HashSet();
        DecimalFormat decimalFormat = new DecimalFormat("0.00");
        List<HalfHourPowerMeterSummaryEntity> hfList = powerMeterSummaryMapper.selectHourPrice(energyDTO);

        Map<String, List<HalfHourPowerMeterSummaryEntity>> hfMap = hfList.stream().collect(Collectors.groupingBy(HalfHourPowerMeterSummaryEntity::getMeterId));

        for (Map map : stringObjMoudelMap) {
            String id = (String) map.get("id");
            String parentId = (String) map.get("parentId");
            String name = (String) map.get("name");

            //拿到为电的计算公式
            if (map.get("电") != null && !"".equals(map.get("电"))) {
                String electricity = (String) map.get("电");
                energyDTO.setRateType("电");
                //格式化计算公式
                String s = MyExpressionUtil.quChu(electricity);
                //拿到计算公式里的所有deviceId   [DHU0001,DHU0002]
                List<String> expressionParams = MyExpressionUtil.getExpressionParams(electricity);
                //循环拿到deviceId
                for (String index : expressionParams) {
                    deviceIdSet.add(index);
                }
                //用查到的公式里的deviceId 和 energyDTO里的参数去查所有数据
//                energyDTO.setIds(list);
                List<HalfHourPowerMeterSummaryEntity> resultList = new ArrayList<>(8);
                expressionParams.forEach(e -> {
                    List<HalfHourPowerMeterSummaryEntity> deviceDataList = hfMap.get(e);
                    if (CollectionUtil.isNotEmpty(deviceDataList)) {
                        resultList.addAll(deviceDataList);
                    }
                });


                //查出来所有数据 按时间进行分组
                Map<String, List<HalfHourPowerMeterSummaryEntity>> collect = resultList.stream().
                        collect(Collectors.groupingBy(HalfHourPowerMeterSummaryEntity::getStartTime));

                //一组list 属于同一个时间段
                for (Map.Entry<String, List<HalfHourPowerMeterSummaryEntity>> entry : collect.entrySet()) {
                    //一组List<HalfHourPowerMeterSummaryEntity> 属于同一个时间段
                    List<HalfHourPowerMeterSummaryEntity> value = entry.getValue();
                    HashMap<String, Object> mapWyf = new HashMap<>();
                    HashMap<String, Object> mapCxk = new HashMap<>();

                    HashMap<String, Object> mapLh = new HashMap<>();
                    HashMap<String, Object> mapYy = new HashMap<>();
                    //峰
                    HashMap<String, Object> mapPeekValue = new HashMap<>();
                    HashMap<String, Object> mapPeekValues = new HashMap<>();
                    //平
                    HashMap<String, Object> mapFlatValue = new HashMap<>();
                    HashMap<String, Object> mapFlatValues = new HashMap<>();
                    //谷
                    HashMap<String, Object> mapValleyValue = new HashMap<>();
                    HashMap<String, Object> mapValleyValues = new HashMap<>();
                    PowerVo powerVo = new PowerVo();
                    String time = "";
                    String rateType = "";

                    //把设备id 对应的能耗放进map  把时间和类型放到对象里
                    for (HalfHourPowerMeterSummaryEntity hs : value) {
                        //费用
                        mapLh.put(hs.getMeterId(), hs.getTotalPrice());
                        //能耗
                        mapWyf.put(hs.getMeterId(), hs.getDetActiveEnergy());
                        //峰
                        mapPeekValue.put(hs.getMeterId(), hs.getPeekValue());
                        //平
                        mapFlatValue.put(hs.getMeterId(), hs.getFlatValue());
                        //谷
                        mapValleyValue.put(hs.getMeterId(), hs.getValleyValue());

                        time = hs.getStartTime();
                        rateType = hs.getRateType();
                        hashSet.add(hs.getStartTime());

                    }
                    //把设备id拼接ll 对应能耗放进对应id里 有设备id的 没有能耗的放0
                    //[DHU0011,DHU0012]
                    //map{"DHU0012","123321"}
                    for (Object ls : expressionParams) {
                        for (Map.Entry<String, Object> stringObjectEntry : mapWyf.entrySet()) {
                            if (!ls.equals(stringObjectEntry.getKey())) {
                                mapCxk.put("LL" + ls, 0);

                            }
                            if (ls.equals(stringObjectEntry.getKey())) {
                                mapCxk.put("LL" + ls, stringObjectEntry.getValue());
                                break;
                            }
                        }
                    }

                    //把设备id拼接ll 对应费率放进对应id里 有设备id的 没有费率的放0
                    //[DHU0011,DHU0012]
                    //map{"DHU0012","123321"}
                    for (Object ls : expressionParams) {
                        for (Map.Entry<String, Object> stringObjectEntry : mapLh.entrySet()) {
                            if (!ls.equals(stringObjectEntry.getKey())) {
                                mapYy.put("LL" + ls, 0);

                            }
                            if (ls.equals(stringObjectEntry.getKey())) {
                                mapYy.put("LL" + ls, stringObjectEntry.getValue());
                                break;
                            }
                        }
                    }

                    //把设备id拼接ll 对应费率放进对应id里 有设备id的 没有费率的放0
                    //[DHU0011,DHU0012]
                    //map{"DHU0012","123321"}
                    //峰
                    for (Object ls : expressionParams) {
                        for (Map.Entry<String, Object> stringObjectEntry : mapPeekValue.entrySet()) {
                            if (!ls.equals(stringObjectEntry.getKey())) {
                                mapPeekValues.put("LL" + ls, 0);

                            }
                            if (ls.equals(stringObjectEntry.getKey())) {
                                mapPeekValues.put("LL" + ls, stringObjectEntry.getValue());
                                break;
                            }
                        }
                    }
                    //把设备id拼接ll 对应费率放进对应id里 有设备id的 没有费率的放0
                    //[DHU0011,DHU0012]
                    //map{"DHU0012","123321"}
                    //平
                    for (Object ls : expressionParams) {
                        for (Map.Entry<String, Object> stringObjectEntry : mapFlatValue.entrySet()) {
                            if (!ls.equals(stringObjectEntry.getKey())) {
                                mapFlatValues.put("LL" + ls, 0);

                            }
                            if (ls.equals(stringObjectEntry.getKey())) {
                                mapFlatValues.put("LL" + ls, stringObjectEntry.getValue());
                                break;
                            }
                        }
                    }

                    //把设备id拼接ll 对应费率放进对应id里 有设备id的 没有费率的放0
                    //[DHU0011,DHU0012]
                    //map{"DHU0012","123321"}
                    //谷
                    for (Object ls : expressionParams) {
                        for (Map.Entry<String, Object> stringObjectEntry : mapValleyValue.entrySet()) {
                            if (!ls.equals(stringObjectEntry.getKey())) {
                                mapValleyValues.put("LL" + ls, 0);

                            }
                            if (ls.equals(stringObjectEntry.getKey())) {
                                mapValleyValues.put("LL" + ls, stringObjectEntry.getValue());
                                break;
                            }
                        }
                    }

                    //{"llDHU0012+llDHU0011",{LLDHU0012:123},{LLDHU0011:0}}
                    //把上面的公式 和 map放进方法 算出能耗值
                    Float num = Float.parseFloat(ExpressionUtil.eval(s, mapCxk).toString());

                    //费率
                    Float price = Float.parseFloat(ExpressionUtil.eval(s, mapYy).toString());
                    //平均价
                    Float rate = 0.00F;
                    if (num > 0 && price > 0) {
                        rate = (price / num);
                    }
                    //峰
                    Float PeekValue = Float.parseFloat(ExpressionUtil.eval(s, mapPeekValues).toString());
                    //平
                    Float FlatValue = Float.parseFloat(ExpressionUtil.eval(s, mapFlatValues).toString());
                    //谷
                    Float ValleyValue = Float.parseFloat(ExpressionUtil.eval(s, mapValleyValues).toString());
                    //保留小数点两位，采用截断模式
                    num = new BigDecimal(num).setScale(2, RoundingMode.DOWN).floatValue();
                    PeekValue = new BigDecimal(PeekValue).setScale(2, RoundingMode.DOWN).floatValue();
                    FlatValue = new BigDecimal(FlatValue).setScale(2, RoundingMode.DOWN).floatValue();
                    ValleyValue = new BigDecimal(ValleyValue).setScale(2, RoundingMode.DOWN).floatValue();
                    price = new BigDecimal(price).setScale(2, RoundingMode.DOWN).floatValue();
                    rate = new BigDecimal(rate).setScale(2, RoundingMode.DOWN).floatValue();
                    //小数点不足两位添0
                    String numStr = decimalFormat.format(num);
                    String rates = decimalFormat.format(rate);
                    String prices = decimalFormat.format(price);
                    String ValleyValues = decimalFormat.format(ValleyValue);
                    String FlatValues = decimalFormat.format(FlatValue);
                    String PeekValues = decimalFormat.format(PeekValue);
                    //把所有需要的值全放入powerVo这个对象
                    powerVo.setRate(rates);
                    powerVo.setNum(numStr);
                    powerVo.setPrice(prices);
                    powerVo.setRateType(rateType);
                    powerVo.setId(id);
                    powerVo.setParentId(parentId);
                    powerVo.setName(name);
                    powerVo.setTime(time);
                    powerVo.setPeekValue(PeekValues);
                    powerVo.setFlatValue(FlatValues);
                    powerVo.setValleyValue(ValleyValues);
                    powerVo.setGraininess(energyDTO.getGraininess());

                    //我这个 listTree 已经是分好组的 数据 就是要遍历 这个 listTree 找出父子级关系 然后 比对时间  如果是父子级id 就比较
                    listTree.add(powerVo);
                }
            } else {
                PowerVo powerVo = new PowerVo();
                //把没有计算公式的放到这个对象集合里
                powerVo.setId(id);
                powerVo.setParentId(parentId);
                powerVo.setName(name);
                powerVo.setGraininess(energyDTO.getGraininess());
                listPower.add(powerVo);
            }
            //如果有  没有计算公式的 就遍历这个对象集合 给每个时间都put进去
            if (listPower.size() > 0) {
                for (PowerVo powerVo : listPower) {
                    for (String setTime : hashSet) {
                        PowerVo powerVo1 = new PowerVo();
                        powerVo1.setId(powerVo.getId());
                        powerVo1.setParentId(powerVo.getParentId());
                        powerVo1.setGraininess(powerVo.getGraininess());
                        powerVo1.setRateType("");
                        powerVo1.setName(powerVo.getName());
                        powerVo1.setTime(setTime);
                        listTree.add(powerVo1);
                    }
                }
            }
        }

        return listTree;
    }


    public List<PowerVo> pageExcel(EnergyDTO energyDTO) {
        List<Map<String, String>> stringObjMoudelMap = objMoudelService.selectMetering(energyDTO);
        List<PowerVo> listTree = new ArrayList<>();
//        List<PowerVo> listPower = new ArrayList<>();
        Map<Integer, PowerVo> mapPower = new HashMap<>();
        int k = 0;
        HashSet<String> hashSet = new HashSet();
        DecimalFormat decimalFormat = new DecimalFormat("0.00");
//        if (energyDTO.getRateType().equals("电")) {
//            energyDTO.setTable("half_hour_power_meter_summary");
//        } else if (energyDTO.getRateType().equals("水")) {
//            energyDTO.setTable("half_hour_flow_water_summary");
//        } else if (energyDTO.getRateType().equals("天然气")) {
//            energyDTO.setTable("half_hour_natural_gas_summary");
//        } else if (energyDTO.getRateType().equals("煤气")) {
//            energyDTO.setTable("half_hour_vapour_summary");
//        }

        List<HalfHourPowerMeterSummaryEntity> hfList = powerMeterSummaryMapper.selectHourPrice(energyDTO);

        Map<String, List<HalfHourPowerMeterSummaryEntity>> hfMap = hfList.stream().collect(Collectors.groupingBy(HalfHourPowerMeterSummaryEntity::getMeterId));

        for (HalfHourPowerMeterSummaryEntity hs : hfList) {
            hashSet.add(hs.getStartTime());
        }

        for (Map map : stringObjMoudelMap) {
            String id = (String) map.get("id");
            String parentId = (String) map.get("parentId");
            String name = (String) map.get("name");

            //拿到为电的计算公式
            if (map.get("电") != null && !"".equals(map.get("电"))) {
                String electricity = (String) map.get("电");
                energyDTO.setRateType("电");
                //格式化计算公式
                String s = MyExpressionUtil.quChu(electricity);
                //拿到计算公式里的所有deviceId   [DHU0001,DHU0002]
                List<String> expressionParams = MyExpressionUtil.getExpressionParams(electricity);
                //循环拿到deviceId
                for (String index : expressionParams) {
                    deviceIdSet.add(index);
                }
                //用查到的公式里的deviceId 和 energyDTO里的参数去查所有数据
//                energyDTO.setIds(list);
                List<HalfHourPowerMeterSummaryEntity> resultList = new ArrayList<>(8);
                expressionParams.forEach(e -> {
                    List<HalfHourPowerMeterSummaryEntity> deviceDataList = hfMap.get(e);
                    if (CollectionUtil.isNotEmpty(deviceDataList)) {
                        resultList.addAll(deviceDataList);
                    }
                });


                //查出来所有数据 按时间进行分组
                Map<String, List<HalfHourPowerMeterSummaryEntity>> collect = resultList.stream().
                        collect(Collectors.groupingBy(HalfHourPowerMeterSummaryEntity::getStartTime));

                //一组list 属于同一个时间段
                for (Map.Entry<String, List<HalfHourPowerMeterSummaryEntity>> entry : collect.entrySet()) {
                    //一组List<HalfHourPowerMeterSummaryEntity> 属于同一个时间段
                    List<HalfHourPowerMeterSummaryEntity> value = entry.getValue();
                    HashMap<String, Object> mapWyf = new HashMap<>();
                    HashMap<String, Object> mapCxk = new HashMap<>();

                    HashMap<String, Object> mapLh = new HashMap<>();
                    HashMap<String, Object> mapYy = new HashMap<>();
                    //峰
                    HashMap<String, Object> mapPeekValue = new HashMap<>();
                    HashMap<String, Object> mapPeekValues = new HashMap<>();
                    //平
                    HashMap<String, Object> mapFlatValue = new HashMap<>();
                    HashMap<String, Object> mapFlatValues = new HashMap<>();
                    //谷
                    HashMap<String, Object> mapValleyValue = new HashMap<>();
                    HashMap<String, Object> mapValleyValues = new HashMap<>();
                    PowerVo powerVo = new PowerVo();
                    String time = "";
                    String rateType = "";

                    //把设备id 对应的能耗放进map  把时间和类型放到对象里
                    for (HalfHourPowerMeterSummaryEntity hs : value) {
                        //费用
                        mapLh.put(hs.getMeterId(), hs.getTotalPrice());
                        //能耗
                        mapWyf.put(hs.getMeterId(), hs.getDetActiveEnergy());
                        //峰
                        mapPeekValue.put(hs.getMeterId(), hs.getPeekValue());
                        //平
                        mapFlatValue.put(hs.getMeterId(), hs.getFlatValue());
                        //谷
                        mapValleyValue.put(hs.getMeterId(), hs.getValleyValue());

                        time = hs.getStartTime();
                        rateType = hs.getRateType();
//                        hashSet.add(hs.getStartTime());

                    }
                    //把设备id拼接ll 对应能耗放进对应id里 有设备id的 没有能耗的放0
                    //[DHU0011,DHU0012]
                    //map{"DHU0012","123321"}
                    for (Object ls : expressionParams) {
                        for (Map.Entry<String, Object> stringObjectEntry : mapWyf.entrySet()) {
                            if (!ls.equals(stringObjectEntry.getKey())) {
                                mapCxk.put("LL" + ls, 0);

                            }
                            if (ls.equals(stringObjectEntry.getKey())) {
                                mapCxk.put("LL" + ls, stringObjectEntry.getValue());
                                break;
                            }
                        }
                    }

                    //把设备id拼接ll 对应费率放进对应id里 有设备id的 没有费率的放0
                    //[DHU0011,DHU0012]
                    //map{"DHU0012","123321"}
                    for (Object ls : expressionParams) {
                        for (Map.Entry<String, Object> stringObjectEntry : mapLh.entrySet()) {
                            if (!ls.equals(stringObjectEntry.getKey())) {
                                mapYy.put("LL" + ls, 0);

                            }
                            if (ls.equals(stringObjectEntry.getKey())) {
                                mapYy.put("LL" + ls, stringObjectEntry.getValue());
                                break;
                            }
                        }
                    }

                    //把设备id拼接ll 对应费率放进对应id里 有设备id的 没有费率的放0
                    //[DHU0011,DHU0012]
                    //map{"DHU0012","123321"}
                    //峰
                    for (Object ls : expressionParams) {
                        for (Map.Entry<String, Object> stringObjectEntry : mapPeekValue.entrySet()) {
                            if (!ls.equals(stringObjectEntry.getKey())) {
                                mapPeekValues.put("LL" + ls, 0);

                            }
                            if (ls.equals(stringObjectEntry.getKey())) {
                                mapPeekValues.put("LL" + ls, stringObjectEntry.getValue());
                                break;
                            }
                        }
                    }
                    //把设备id拼接ll 对应费率放进对应id里 有设备id的 没有费率的放0
                    //[DHU0011,DHU0012]
                    //map{"DHU0012","123321"}
                    //平
                    for (Object ls : expressionParams) {
                        for (Map.Entry<String, Object> stringObjectEntry : mapFlatValue.entrySet()) {
                            if (!ls.equals(stringObjectEntry.getKey())) {
                                mapFlatValues.put("LL" + ls, 0);

                            }
                            if (ls.equals(stringObjectEntry.getKey())) {
                                mapFlatValues.put("LL" + ls, stringObjectEntry.getValue());
                                break;
                            }
                        }
                    }

                    //把设备id拼接ll 对应费率放进对应id里 有设备id的 没有费率的放0
                    //[DHU0011,DHU0012]
                    //map{"DHU0012","123321"}
                    //谷
                    for (Object ls : expressionParams) {
                        for (Map.Entry<String, Object> stringObjectEntry : mapValleyValue.entrySet()) {
                            if (!ls.equals(stringObjectEntry.getKey())) {
                                mapValleyValues.put("LL" + ls, 0);

                            }
                            if (ls.equals(stringObjectEntry.getKey())) {
                                mapValleyValues.put("LL" + ls, stringObjectEntry.getValue());
                                break;
                            }
                        }
                    }

                    //{"llDHU0012+llDHU0011",{LLDHU0012:123},{LLDHU0011:0}}
                    //把上面的公式 和 map放进方法 算出能耗值
                    Float num = Float.parseFloat(ExpressionUtil.eval(s, mapCxk).toString());

                    //费率
                    Float price = Float.parseFloat(ExpressionUtil.eval(s, mapYy).toString());
                    //平均价
                    Float rate = 0.00F;
                    if (num > 0 && price > 0) {
                        rate = (price / num);
                    }
                    //峰
                    Float PeekValue = Float.parseFloat(ExpressionUtil.eval(s, mapPeekValues).toString());
                    //平
                    Float FlatValue = Float.parseFloat(ExpressionUtil.eval(s, mapFlatValues).toString());
                    //谷
                    Float ValleyValue = Float.parseFloat(ExpressionUtil.eval(s, mapValleyValues).toString());
                    //保留小数点两位，采用截断模式
                    num = new BigDecimal(num).setScale(2, RoundingMode.DOWN).floatValue();
                    PeekValue = new BigDecimal(PeekValue).setScale(2, RoundingMode.DOWN).floatValue();
                    FlatValue = new BigDecimal(FlatValue).setScale(2, RoundingMode.DOWN).floatValue();
                    ValleyValue = new BigDecimal(ValleyValue).setScale(2, RoundingMode.DOWN).floatValue();
                    price = new BigDecimal(price).setScale(2, RoundingMode.DOWN).floatValue();
                    rate = new BigDecimal(rate).setScale(2, RoundingMode.DOWN).floatValue();
                    //小数点不足两位添0
                    String numStr = decimalFormat.format(num);
                    String rates = decimalFormat.format(rate);
                    String prices = decimalFormat.format(price);
                    String ValleyValues = decimalFormat.format(ValleyValue);
                    String FlatValues = decimalFormat.format(FlatValue);
                    String PeekValues = decimalFormat.format(PeekValue);
                    //把所有需要的值全放入powerVo这个对象
                    powerVo.setRate(rates);
                    powerVo.setNum(numStr);
                    powerVo.setPrice(prices);
                    powerVo.setRateType(rateType);
                    powerVo.setId(id);
                    powerVo.setParentId(parentId);
                    powerVo.setName(name);
                    powerVo.setTime(time);
                    powerVo.setPeekValue(PeekValues);
                    powerVo.setFlatValue(FlatValues);
                    powerVo.setValleyValue(ValleyValues);
                    powerVo.setGraininess(energyDTO.getGraininess());

                    //我这个 listTree 已经是分好组的 数据 就是要遍历 这个 listTree 找出父子级关系 然后 比对时间  如果是父子级id 就比较
                    listTree.add(powerVo);
                }
            } else {
                PowerVo powerVo = new PowerVo();
                //把没有计算公式的放到这个对象集合里
                powerVo.setId(id);
                powerVo.setParentId(parentId);
                powerVo.setName(name);
                powerVo.setGraininess(energyDTO.getGraininess());
                powerVo.setPrice("0.00");
                powerVo.setNum("0.00");
                powerVo.setRateType(energyDTO.getRateType());
//                listPower.add(powerVo);
                mapPower.put(k, powerVo);
            }
            //如果有  没有计算公式的 就遍历这个对象集合 给每个时间都put进去
            if (mapPower.size() > 0) {
                for (int key : mapPower.keySet()) {
                    PowerVo powerVo = mapPower.get(key);
                    for (String setTime : hashSet) {
                        PowerVo powerVo1 = new PowerVo();
                        powerVo1.setId(powerVo.getId());
                        powerVo1.setParentId(powerVo.getParentId());
                        powerVo1.setGraininess(powerVo.getGraininess());
                        powerVo1.setRateType(energyDTO.getRateType());
                        powerVo1.setPrice("0.00(暂无计算公式)");
                        powerVo1.setNum("0.00(暂无计算公式)");
                        powerVo1.setName(powerVo.getName());
                        powerVo1.setTime(setTime);
                        listTree.add(powerVo1);
                    }
                }
                mapPower.clear();
            }
            k++;
        }
        return listTree;
    }


    /**
     * 成本和能耗 计算树
     */
    @Override
    public Page<Map<String, Object>> selectHourPrice(PageDTO pageDTO) {
        EnergyDTO energyDTO = (EnergyDTO) pageDTO.getDto();
        EnergyDTO energyDTO1 = new EnergyDTO();
        BeanUtils.copyProperties(energyDTO, energyDTO1);
        List<Map<String, Object>> lisAllTree = new ArrayList<>();
        List<Map<String, Object>> lisAllTreeFinal = new ArrayList<>();
        Page<Map<String, Object>> page = new Page<>();
        deviceIdSet = new HashSet<>();
        long total = 0;
//        设置小数点不足两位添0格式
        DecimalFormat decimalFormat = new DecimalFormat("0.00");
//        每次设置查询时间间隔为5小时并且查询一次之后会开始结束时间等会往前平移5小时，查出10或大于棵树跳出循环
        String endTime = energyDTO.getEndTime();
        String startTime = energyDTO.getStartTime();
        //获取有数据得最末时间
        endTime = getFirstDate(energyDTO);
        endTime = endTime.compareTo(startTime) == -1 ? startTime : endTime;
        startTime = startTime + " 00:00:00";
        endTime = endTime + " 00:00:00";
        DateTime dateTimeEnd = DateUtil.parse(endTime, "yyyy-MM-dd HH:mm");
        DateTime startTimeInitial = DateUtil.parse(startTime, "yyyy-MM-dd HH:mm");
        //        通过偏移量获取最后时间
        int off = (int) (pageDTO.getSize() * (pageDTO.getPage() - 1));
        DateTime dateTimeStart = null;
        int size = (int) pageDTO.getSize();
        switch (energyDTO.getGraininess()) {
//            查询半小时
            case "1":
                dateTimeEnd = DateUtil.offset(dateTimeEnd, DateField.DAY_OF_WEEK, 1);
                dateTimeEnd = DateUtil.offset(dateTimeEnd, DateField.HOUR, -off / 2);
                dateTimeStart = DateUtil.offset(dateTimeEnd, DateField.HOUR, -size / 2);
                dateTimeStart = ChronoUnit.HOURS.between(startTimeInitial.toInstant(), dateTimeStart.toInstant()) >= 0 ? dateTimeStart : startTimeInitial;
                energyDTO.setStartTime(DateUtil.format(dateTimeStart, "yyyy-MM-dd HH:mm"));
                energyDTO.setEndTime(DateUtil.format(dateTimeEnd, "yyyy-MM-dd HH:mm"));
                break;
//                天
            case "2":
                dateTimeEnd = DateUtil.offset(dateTimeEnd, DateField.DAY_OF_WEEK, -off);
                dateTimeStart = DateUtil.offset(dateTimeEnd, DateField.DAY_OF_WEEK, -size);
                dateTimeStart = ChronoUnit.HOURS.between(startTimeInitial.toInstant(), dateTimeStart.toInstant()) >= 0 ? dateTimeStart : startTimeInitial;
                energyDTO.setStartTime(DateUtil.format(dateTimeStart, "yyyy-MM-dd"));
                energyDTO.setEndTime(DateUtil.format(dateTimeEnd, "yyyy-MM-dd"));
                break;
//                周
            case "3":
                dateTimeEnd = DateUtil.offset(dateTimeEnd, DateField.WEEK_OF_MONTH, -off);
                dateTimeStart = DateUtil.offset(dateTimeEnd, DateField.WEEK_OF_MONTH, -10);
                dateTimeStart = ChronoUnit.HOURS.between(startTimeInitial.toInstant(), dateTimeStart.toInstant()) >= 0 ? dateTimeStart : startTimeInitial;
                energyDTO.setStartTime(DateUtil.format(dateTimeStart, "yyyy-MM-dd"));
                energyDTO.setEndTime(DateUtil.format(dateTimeEnd, "yyyy-MM-dd"));
                break;
//                月
            case "4":
                dateTimeEnd = DateUtil.offset(dateTimeEnd, DateField.MONTH, -off);
                dateTimeStart = DateUtil.offset(dateTimeEnd, DateField.MONTH, -size);
                dateTimeStart = ChronoUnit.HOURS.between(startTimeInitial.toInstant(), dateTimeStart.toInstant()) >= 0 ? dateTimeStart : startTimeInitial;
                energyDTO.setStartTime(DateUtil.format(dateTimeStart, "yyyy-MM-dd"));
                energyDTO.setEndTime(DateUtil.format(dateTimeEnd, "yyyy-MM-dd"));
                break;
//                年
            default:
                dateTimeEnd = DateUtil.offset(dateTimeEnd, DateField.YEAR, -off);
                dateTimeStart = DateUtil.offset(dateTimeEnd, DateField.YEAR, -size);
                dateTimeStart = ChronoUnit.HOURS.between(startTimeInitial.toInstant(), dateTimeStart.toInstant()) >= 0 ? dateTimeStart : startTimeInitial;
                energyDTO.setStartTime(DateUtil.format(dateTimeStart, "yyyy-MM-dd"));
                energyDTO.setEndTime(DateUtil.format(dateTimeEnd, "yyyy-MM-dd"));
                break;
        }
        boolean flag = true;
        int checkCount = 0;
        String format = null;
        while (lisAllTreeFinal.size() < pageDTO.getSize() && ChronoUnit.HOURS.between(startTimeInitial.toInstant(), dateTimeEnd.toInstant()) >= 0) {
            if (energyDTO.getGraininess().equals("1")) {
                //遍历 拿到设备 电的计算规则
                List<PowerVo> listTree = page(energyDTO);
                //把 listTree 按世间分组
                Map<String, List<PowerVo>> collectPower = listTree.stream().collect(Collectors.groupingBy(PowerVo::getTime));
                PowerMeterSummaryUtils ps = new PowerMeterSummaryUtils();
                //把分组的集合 和 参数放入递归
                List<Map<String, Object>> time = ps.PowerTree(collectPower, energyDTO);
                //递归返回出来的 time进行正序排列 获得 listAllTree
                lisAllTree = time.stream().sorted(Comparator
                                .comparing((Map<String, Object> map) ->
                                        new DateTime(map.get("time").toString())).reversed())
                        .collect(Collectors.toList());
            } else {
                //遍历 拿到设备 电的计算规则
                List<Map<String, String>> stringObjMoudelMap = objMoudelService.selectMetering(energyDTO);
                List<PowerVo> listTree = new ArrayList<>();
                List<PowerVo> listPower = new ArrayList<>();
                HashSet<String> hashSet = new HashSet();

                List<HalfHourPowerMeterSummaryEntity> hfList = powerMeterSummaryMapper.selectDatePricePage(energyDTO);
                Map<String, List<HalfHourPowerMeterSummaryEntity>> hfMap = hfList.stream().collect(Collectors.groupingBy(HalfHourPowerMeterSummaryEntity::getMeterId));

                for (Map map : stringObjMoudelMap) {
                    String id = (String) map.get("id");
                    String parentId = (String) map.get("parentId");
                    String name = (String) map.get("name");
                    //拿到为电的计算公式
                    if (map.get("电") != null && !"".equals(map.get("电"))) {
                        String electricity = (String) map.get("电");
                        energyDTO.setRateType("电");
                        //格式化计算公式
                        String s = MyExpressionUtil.quChu(electricity);
                        //拿到计算公式里的所有deviceId

                        List<String> expressionParams = MyExpressionUtil.getExpressionParams(electricity);
                        //循环拿到deviceId
                        List list = new ArrayList();
                        for (String index : expressionParams) {
                            list.add(index);
                            deviceIdSet.add(index);
                        }
                        //energyDTO.setIds(list);


                        List<HalfHourPowerMeterSummaryEntity> resultList = new ArrayList<>(8);
                        expressionParams.forEach(e -> {
                            List<HalfHourPowerMeterSummaryEntity> deviceDataList = hfMap.get(e);
                            if (CollectionUtil.isNotEmpty(deviceDataList)) {
                                resultList.addAll(deviceDataList);
                            }
                        });

                        Map<String, List<HalfHourPowerMeterSummaryEntity>> collect = new HashMap<>();
                        if (energyDTO.getGraininess().equals("2")) {
                            collect = resultList.stream().
                                    collect(Collectors.groupingBy(HalfHourPowerMeterSummaryEntity::getDayStr));
                            for (Map.Entry<String, List<HalfHourPowerMeterSummaryEntity>> entry : collect.entrySet()) {

                                List<HalfHourPowerMeterSummaryEntity> value = entry.getValue();
                                //树的根节点  第一层的所有时间段数据  一个时间段就代表一个树的根节点
                                HashMap<String, Object> mapWyf = new HashMap<>();
                                HashMap<String, Object> mapCxk = new HashMap<>();

                                HashMap<String, Object> mapLh = new HashMap<>();
                                HashMap<String, Object> mapYy = new HashMap<>();

                                //峰
                                HashMap<String, Object> mapPeekValue = new HashMap<>();
                                HashMap<String, Object> mapPeekValues = new HashMap<>();
                                //平
                                HashMap<String, Object> mapFlatValue = new HashMap<>();
                                HashMap<String, Object> mapFlatValues = new HashMap<>();
                                //谷
                                HashMap<String, Object> mapValleyValue = new HashMap<>();
                                HashMap<String, Object> mapValleyValues = new HashMap<>();

                                PowerVo powerVo = new PowerVo();
                                String time = "";
                                String rateType = "";

                                //把设备id 对应的能耗放进map  把时间和类型放到对象里
                                for (HalfHourPowerMeterSummaryEntity hs : value) {
                                    //费用
                                    mapLh.put(hs.getMeterId(), hs.getTotalPrice());
                                    //能耗
                                    mapWyf.put(hs.getMeterId(), hs.getDetActiveEnergy());
                                    //峰
                                    mapPeekValue.put(hs.getMeterId(), hs.getPeekValue());
                                    //平
                                    mapFlatValue.put(hs.getMeterId(), hs.getFlatValue());
                                    //谷
                                    mapValleyValue.put(hs.getMeterId(), hs.getValleyValue());


                                    time = hs.getDayStr();
                                    rateType = hs.getRateType();
                                    hashSet.add(hs.getDayStr());

                                }
                                //把设备id拼接ll 对应能耗放进对应id里 有设备id的 没有能耗的放0
                                //[DHU0011,DHU0012]
                                //map{"DHU0012","123321"}
                                for (Object ls : list) {
                                    for (Map.Entry<String, Object> stringObjectEntry : mapWyf.entrySet()) {
                                        if (!ls.equals(stringObjectEntry.getKey())) {
                                            mapCxk.put("LL" + ls, 0);

                                        }
                                        if (ls.equals(stringObjectEntry.getKey())) {
                                            mapCxk.put("LL" + ls, stringObjectEntry.getValue());
                                            break;
                                        }
                                    }
                                }

                                //把设备id拼接ll 对应费率放进对应id里 有设备id的 没有费率的放0
                                //[DHU0011,DHU0012]
                                //map{"DHU0012","123321"}
                                for (Object ls : list) {
                                    for (Map.Entry<String, Object> stringObjectEntry : mapLh.entrySet()) {
                                        if (!ls.equals(stringObjectEntry.getKey())) {
                                            mapYy.put("LL" + ls, 0);

                                        }
                                        if (ls.equals(stringObjectEntry.getKey())) {
                                            mapYy.put("LL" + ls, stringObjectEntry.getValue());
                                            break;
                                        }
                                    }
                                }

                                //把设备id拼接ll 对应费率放进对应id里 有设备id的 没有费率的放0
                                //[DHU0011,DHU0012]
                                //map{"DHU0012","123321"}
                                //峰
                                for (Object ls : list) {
                                    for (Map.Entry<String, Object> stringObjectEntry : mapPeekValue.entrySet()) {
                                        if (!ls.equals(stringObjectEntry.getKey())) {
                                            mapPeekValues.put("LL" + ls, 0);

                                        }
                                        if (ls.equals(stringObjectEntry.getKey())) {
                                            mapPeekValues.put("LL" + ls, stringObjectEntry.getValue());
                                            break;
                                        }
                                    }
                                }
                                //把设备id拼接ll 对应费率放进对应id里 有设备id的 没有费率的放0
                                //[DHU0011,DHU0012]
                                //map{"DHU0012","123321"}
                                //平
                                for (Object ls : list) {
                                    for (Map.Entry<String, Object> stringObjectEntry : mapFlatValue.entrySet()) {
                                        if (!ls.equals(stringObjectEntry.getKey())) {
                                            mapFlatValues.put("LL" + ls, 0);

                                        }
                                        if (ls.equals(stringObjectEntry.getKey())) {
                                            mapFlatValues.put("LL" + ls, stringObjectEntry.getValue());
                                            break;
                                        }
                                    }
                                }

                                //把设备id拼接ll 对应费率放进对应id里 有设备id的 没有费率的放0
                                //[DHU0011,DHU0012]
                                //map{"DHU0012","123321"}
                                //谷
                                for (Object ls : list) {
                                    for (Map.Entry<String, Object> stringObjectEntry : mapValleyValue.entrySet()) {
                                        if (!ls.equals(stringObjectEntry.getKey())) {
                                            mapValleyValues.put("LL" + ls, 0);

                                        }
                                        if (ls.equals(stringObjectEntry.getKey())) {
                                            mapValleyValues.put("LL" + ls, stringObjectEntry.getValue());
                                            break;
                                        }
                                    }
                                }


                                //{"llDHU0012+llDHU0011",{LLDHU0012:123},{LLDHU0011:0}}
                                //把上面的公式 和 map放进方法 算出能耗值
                                Float num = Float.parseFloat(ExpressionUtil.eval(s, mapCxk).toString());
                                //费率
                                Float price = Float.parseFloat(ExpressionUtil.eval(s, mapYy).toString());

                                //峰
                                Float PeekValue = Float.parseFloat(ExpressionUtil.eval(s, mapPeekValues).toString());
                                //平
                                Float FlatValue = Float.parseFloat(ExpressionUtil.eval(s, mapFlatValues).toString());
                                //谷
                                Float ValleyValue = Float.parseFloat(ExpressionUtil.eval(s, mapValleyValues).toString());
                                //平均价
                                Float rate = 0.00F;
                                if (num > 0 && price > 0) {
                                    rate = (price / num);
                                }
                                //保留小数点两位，采用截断模式
                                num = new BigDecimal(num).setScale(2, RoundingMode.DOWN).floatValue();
                                PeekValue = new BigDecimal(PeekValue).setScale(2, RoundingMode.DOWN).floatValue();
                                FlatValue = new BigDecimal(FlatValue).setScale(2, RoundingMode.DOWN).floatValue();
                                ValleyValue = new BigDecimal(ValleyValue).setScale(2, RoundingMode.DOWN).floatValue();
                                price = new BigDecimal(price).setScale(2, RoundingMode.DOWN).floatValue();
                                rate = new BigDecimal(rate).setScale(2, RoundingMode.DOWN).floatValue();
                                //小数点不足两位添0
                                String numStr = decimalFormat.format(num);
                                String rates = decimalFormat.format(rate);
                                String prices = decimalFormat.format(price);
                                String ValleyValues = decimalFormat.format(ValleyValue);
                                String FlatValues = decimalFormat.format(FlatValue);
                                String PeekValues = decimalFormat.format(PeekValue);
                                //把所有需要的值全放入powerVo这个对象
                                powerVo.setRate(rates);
                                powerVo.setNum(numStr);
                                powerVo.setPrice(prices);
                                powerVo.setRateType(rateType);
                                powerVo.setId(id);
                                powerVo.setParentId(parentId);
                                powerVo.setName(name);
                                powerVo.setTime(time);

                                powerVo.setPeekValue(PeekValues);
                                powerVo.setFlatValue(FlatValues);
                                powerVo.setValleyValue(ValleyValues);
                                powerVo.setGraininess(energyDTO.getGraininess());

                                //我这个 listTree 已经是分好组的 数据 就是要遍历 这个 listTree 找出父子级关系 然后 比对时间  如果是父子级id 就比较
                                listTree.add(powerVo);
                            }
                        } else if (energyDTO.getGraininess().equals("3")) {
                            collect = resultList.stream().
                                    collect(Collectors.groupingBy(HalfHourPowerMeterSummaryEntity::getWeekStr));
                            for (Map.Entry<String, List<HalfHourPowerMeterSummaryEntity>> entry : collect.entrySet()) {

                                List<HalfHourPowerMeterSummaryEntity> value = entry.getValue();
                                //树的根节点  第一层的所有时间段数据  一个时间段就代表一个树的根节点
                                HashMap<String, Object> mapWyf = new HashMap<>();
                                HashMap<String, Object> mapCxk = new HashMap<>();

                                HashMap<String, Object> mapLh = new HashMap<>();
                                HashMap<String, Object> mapYy = new HashMap<>();

                                //峰
                                HashMap<String, Object> mapPeekValue = new HashMap<>();
                                HashMap<String, Object> mapPeekValues = new HashMap<>();
                                //平
                                HashMap<String, Object> mapFlatValue = new HashMap<>();
                                HashMap<String, Object> mapFlatValues = new HashMap<>();
                                //谷
                                HashMap<String, Object> mapValleyValue = new HashMap<>();
                                HashMap<String, Object> mapValleyValues = new HashMap<>();

                                PowerVo powerVo = new PowerVo();
                                String time = "";
                                String rateType = "";

                                //把设备id 对应的能耗放进map  把时间和类型放到对象里
                                for (HalfHourPowerMeterSummaryEntity hs : value) {
                                    //费用
                                    mapLh.put(hs.getMeterId(), hs.getTotalPrice());
                                    //能耗
                                    mapWyf.put(hs.getMeterId(), hs.getDetActiveEnergy());
                                    //峰
                                    mapPeekValue.put(hs.getMeterId(), hs.getPeekValue());
                                    //平
                                    mapFlatValue.put(hs.getMeterId(), hs.getFlatValue());
                                    //谷
                                    mapValleyValue.put(hs.getMeterId(), hs.getValleyValue());

                                    time = hs.getWeekStr();
                                    rateType = hs.getRateType();
                                    hashSet.add(hs.getWeekStr());

                                }
                                //把设备id拼接ll 对应能耗放进对应id里 有设备id的 没有能耗的放0
                                //[DHU0011,DHU0012]
                                //map{"DHU0012","123321"}
                                for (Object ls : list) {
                                    for (Map.Entry<String, Object> stringObjectEntry : mapWyf.entrySet()) {
                                        if (!ls.equals(stringObjectEntry.getKey())) {
                                            mapCxk.put("LL" + ls, 0);

                                        }
                                        if (ls.equals(stringObjectEntry.getKey())) {
                                            mapCxk.put("LL" + ls, stringObjectEntry.getValue());
                                            break;
                                        }
                                    }
                                }

                                //把设备id拼接ll 对应费率放进对应id里 有设备id的 没有费率的放0
                                //[DHU0011,DHU0012]
                                //map{"DHU0012","123321"}
                                for (Object ls : list) {
                                    for (Map.Entry<String, Object> stringObjectEntry : mapLh.entrySet()) {
                                        if (!ls.equals(stringObjectEntry.getKey())) {
                                            mapYy.put("LL" + ls, 0);

                                        }
                                        if (ls.equals(stringObjectEntry.getKey())) {
                                            mapYy.put("LL" + ls, stringObjectEntry.getValue());
                                            break;
                                        }
                                    }
                                }

                                //把设备id拼接ll 对应费率放进对应id里 有设备id的 没有费率的放0
                                //[DHU0011,DHU0012]
                                //map{"DHU0012","123321"}
                                //峰
                                for (Object ls : list) {
                                    for (Map.Entry<String, Object> stringObjectEntry : mapPeekValue.entrySet()) {
                                        if (!ls.equals(stringObjectEntry.getKey())) {
                                            mapPeekValues.put("LL" + ls, 0);

                                        }
                                        if (ls.equals(stringObjectEntry.getKey())) {
                                            mapPeekValues.put("LL" + ls, stringObjectEntry.getValue());
                                            break;
                                        }
                                    }
                                }
                                //把设备id拼接ll 对应费率放进对应id里 有设备id的 没有费率的放0
                                //[DHU0011,DHU0012]
                                //map{"DHU0012","123321"}
                                //平
                                for (Object ls : list) {
                                    for (Map.Entry<String, Object> stringObjectEntry : mapFlatValue.entrySet()) {
                                        if (!ls.equals(stringObjectEntry.getKey())) {
                                            mapFlatValues.put("LL" + ls, 0);

                                        }
                                        if (ls.equals(stringObjectEntry.getKey())) {
                                            mapFlatValues.put("LL" + ls, stringObjectEntry.getValue());
                                            break;
                                        }
                                    }
                                }

                                //把设备id拼接ll 对应费率放进对应id里 有设备id的 没有费率的放0
                                //[DHU0011,DHU0012]
                                //map{"DHU0012","123321"}
                                //谷
                                for (Object ls : list) {
                                    for (Map.Entry<String, Object> stringObjectEntry : mapValleyValue.entrySet()) {
                                        if (!ls.equals(stringObjectEntry.getKey())) {
                                            mapValleyValues.put("LL" + ls, 0);

                                        }
                                        if (ls.equals(stringObjectEntry.getKey())) {
                                            mapValleyValues.put("LL" + ls, stringObjectEntry.getValue());
                                            break;
                                        }
                                    }
                                }


                                //{"llDHU0012+llDHU0011",{LLDHU0012:123},{LLDHU0011:0}}
                                //把上面的公式 和 map放进方法 算出能耗值
                                Float num = Float.parseFloat(ExpressionUtil.eval(s, mapCxk).toString());
                                //费率
                                Float price = Float.parseFloat(ExpressionUtil.eval(s, mapYy).toString());

                                //峰
                                Float PeekValue = Float.parseFloat(ExpressionUtil.eval(s, mapPeekValues).toString());
                                //平
                                Float FlatValue = Float.parseFloat(ExpressionUtil.eval(s, mapFlatValues).toString());
                                //谷
                                Float ValleyValue = Float.parseFloat(ExpressionUtil.eval(s, mapValleyValues).toString());
                                //平均价
                                Float rate = 0.00F;
                                if (num > 0 && price > 0) {
                                    rate = (price / num);
                                }
                                //保留小数点两位，采用截断模式
                                num = new BigDecimal(num).setScale(2, RoundingMode.DOWN).floatValue();
                                PeekValue = new BigDecimal(PeekValue).setScale(2, RoundingMode.DOWN).floatValue();
                                FlatValue = new BigDecimal(FlatValue).setScale(2, RoundingMode.DOWN).floatValue();
                                ValleyValue = new BigDecimal(ValleyValue).setScale(2, RoundingMode.DOWN).floatValue();
                                price = new BigDecimal(price).setScale(2, RoundingMode.DOWN).floatValue();
                                rate = new BigDecimal(rate).setScale(2, RoundingMode.DOWN).floatValue();
                                //小数点不足两位添0
                                String numStr = decimalFormat.format(num);
                                String rates = decimalFormat.format(rate);
                                String prices = decimalFormat.format(price);
                                String ValleyValues = decimalFormat.format(ValleyValue);
                                String FlatValues = decimalFormat.format(FlatValue);
                                String PeekValues = decimalFormat.format(PeekValue);
                                //平均价
                                powerVo.setRate(rates);
                                //能耗
                                powerVo.setNum(numStr);
                                //成本
                                powerVo.setPrice(prices);
                                powerVo.setRateType(rateType);
                                powerVo.setId(id);
                                powerVo.setParentId(parentId);
                                powerVo.setName(name);
                                powerVo.setTime(time);

                                powerVo.setPeekValue(PeekValues);
                                powerVo.setFlatValue(FlatValues);
                                powerVo.setValleyValue(ValleyValues);
                                powerVo.setGraininess(energyDTO.getGraininess());

                                //我这个 listTree 已经是分好组的 数据 就是要遍历 这个 listTree 找出父子级关系 然后 比对时间  如果是父子级id 就比较
                                listTree.add(powerVo);
                            }

                        } else if (energyDTO.getGraininess().equals("4")) {
                            collect = resultList.stream().
                                    collect(Collectors.groupingBy(HalfHourPowerMeterSummaryEntity::getMonthStr));
                            for (Map.Entry<String, List<HalfHourPowerMeterSummaryEntity>> entry : collect.entrySet()) {

                                List<HalfHourPowerMeterSummaryEntity> value = entry.getValue();
                                //树的根节点  第一层的所有时间段数据  一个时间段就代表一个树的根节点
                                HashMap<String, Object> mapWyf = new HashMap<>();
                                HashMap<String, Object> mapCxk = new HashMap<>();

                                HashMap<String, Object> mapLh = new HashMap<>();
                                HashMap<String, Object> mapYy = new HashMap<>();

                                //峰
                                HashMap<String, Object> mapPeekValue = new HashMap<>();
                                HashMap<String, Object> mapPeekValues = new HashMap<>();
                                //平
                                HashMap<String, Object> mapFlatValue = new HashMap<>();
                                HashMap<String, Object> mapFlatValues = new HashMap<>();
                                //谷
                                HashMap<String, Object> mapValleyValue = new HashMap<>();
                                HashMap<String, Object> mapValleyValues = new HashMap<>();

                                PowerVo powerVo = new PowerVo();
                                String time = "";
                                String rateType = "";

                                //把设备id 对应的能耗放进map  把时间和类型放到对象里
                                for (HalfHourPowerMeterSummaryEntity hs : value) {
                                    //费用
                                    mapLh.put(hs.getMeterId(), hs.getTotalPrice());
                                    //能耗
                                    mapWyf.put(hs.getMeterId(), hs.getDetActiveEnergy());
                                    //峰
                                    mapPeekValue.put(hs.getMeterId(), hs.getPeekValue());
                                    //平
                                    mapFlatValue.put(hs.getMeterId(), hs.getFlatValue());
                                    //谷
                                    mapValleyValue.put(hs.getMeterId(), hs.getValleyValue());
                                    time = hs.getMonthStr();
                                    rateType = hs.getRateType();
                                    hashSet.add(hs.getMonthStr());

                                }
                                //把设备id拼接ll 对应能耗放进对应id里 有设备id的 没有能耗的放0
                                //[DHU0011,DHU0012]
                                //map{"DHU0012","123321"}
                                for (Object ls : list) {
                                    for (Map.Entry<String, Object> stringObjectEntry : mapWyf.entrySet()) {
                                        if (!ls.equals(stringObjectEntry.getKey())) {
                                            mapCxk.put("LL" + ls, 0);

                                        }
                                        if (ls.equals(stringObjectEntry.getKey())) {
                                            mapCxk.put("LL" + ls, stringObjectEntry.getValue());
                                            break;
                                        }
                                    }
                                }

                                //把设备id拼接ll 对应费率放进对应id里 有设备id的 没有费率的放0
                                //[DHU0011,DHU0012]
                                //map{"DHU0012","123321"}
                                for (Object ls : list) {
                                    for (Map.Entry<String, Object> stringObjectEntry : mapLh.entrySet()) {
                                        if (!ls.equals(stringObjectEntry.getKey())) {
                                            mapYy.put("LL" + ls, 0);

                                        }
                                        if (ls.equals(stringObjectEntry.getKey())) {
                                            mapYy.put("LL" + ls, stringObjectEntry.getValue());
                                            break;
                                        }
                                    }
                                }

                                //把设备id拼接ll 对应费率放进对应id里 有设备id的 没有费率的放0
                                //[DHU0011,DHU0012]
                                //map{"DHU0012","123321"}
                                //峰
                                for (Object ls : list) {
                                    for (Map.Entry<String, Object> stringObjectEntry : mapPeekValue.entrySet()) {
                                        if (!ls.equals(stringObjectEntry.getKey())) {
                                            mapPeekValues.put("LL" + ls, 0);

                                        }
                                        if (ls.equals(stringObjectEntry.getKey())) {
                                            mapPeekValues.put("LL" + ls, stringObjectEntry.getValue());
                                            break;
                                        }
                                    }
                                }
                                //把设备id拼接ll 对应费率放进对应id里 有设备id的 没有费率的放0
                                //[DHU0011,DHU0012]
                                //map{"DHU0012","123321"}
                                //平
                                for (Object ls : list) {
                                    for (Map.Entry<String, Object> stringObjectEntry : mapFlatValue.entrySet()) {
                                        if (!ls.equals(stringObjectEntry.getKey())) {
                                            mapFlatValues.put("LL" + ls, 0);

                                        }
                                        if (ls.equals(stringObjectEntry.getKey())) {
                                            mapFlatValues.put("LL" + ls, stringObjectEntry.getValue());
                                            break;
                                        }
                                    }
                                }

                                //把设备id拼接ll 对应费率放进对应id里 有设备id的 没有费率的放0
                                //[DHU0011,DHU0012]
                                //map{"DHU0012","123321"}
                                //谷
                                for (Object ls : list) {
                                    for (Map.Entry<String, Object> stringObjectEntry : mapValleyValue.entrySet()) {
                                        if (!ls.equals(stringObjectEntry.getKey())) {
                                            mapValleyValues.put("LL" + ls, 0);

                                        }
                                        if (ls.equals(stringObjectEntry.getKey())) {
                                            mapValleyValues.put("LL" + ls, stringObjectEntry.getValue());
                                            break;
                                        }
                                    }
                                }


                                //{"llDHU0012+llDHU0011",{LLDHU0012:123},{LLDHU0011:0}}
                                //把上面的公式 和 map放进方法 算出能耗值
                                Float num = Float.parseFloat(ExpressionUtil.eval(s, mapCxk).toString());
                                //费率
                                Float price = Float.parseFloat(ExpressionUtil.eval(s, mapYy).toString());

                                //峰
                                Float PeekValue = Float.parseFloat(ExpressionUtil.eval(s, mapPeekValues).toString());
                                //平
                                Float FlatValue = Float.parseFloat(ExpressionUtil.eval(s, mapFlatValues).toString());
                                //谷
                                Float ValleyValue = Float.parseFloat(ExpressionUtil.eval(s, mapValleyValues).toString());

                                //平均价
                                Float rate = 0.00F;
                                if (num > 0 && price > 0) {
                                    rate = (price / num);
                                }
                                //保留小数点两位，采用截断模式
                                num = new BigDecimal(num).setScale(2, RoundingMode.DOWN).floatValue();
                                PeekValue = new BigDecimal(PeekValue).setScale(2, RoundingMode.DOWN).floatValue();
                                FlatValue = new BigDecimal(FlatValue).setScale(2, RoundingMode.DOWN).floatValue();
                                ValleyValue = new BigDecimal(ValleyValue).setScale(2, RoundingMode.DOWN).floatValue();
                                price = new BigDecimal(price).setScale(2, RoundingMode.DOWN).floatValue();
                                rate = new BigDecimal(rate).setScale(2, RoundingMode.DOWN).floatValue();
                                //小数点不足两位添0
                                String numStr = decimalFormat.format(num);
                                String rates = decimalFormat.format(rate);
                                String prices = decimalFormat.format(price);
                                String ValleyValues = decimalFormat.format(ValleyValue);
                                String FlatValues = decimalFormat.format(FlatValue);
                                String PeekValues = decimalFormat.format(PeekValue);
                                //把所有需要的值全放入powerVo这个对象

                                powerVo.setRate(rates);
                                powerVo.setNum(numStr);
                                powerVo.setPrice(prices);
                                powerVo.setRateType(rateType);
                                powerVo.setId(id);
                                powerVo.setParentId(parentId);
                                powerVo.setName(name);
                                powerVo.setTime(time);

                                powerVo.setPeekValue(PeekValues);
                                powerVo.setFlatValue(FlatValues);
                                powerVo.setValleyValue(ValleyValues);
                                powerVo.setGraininess(energyDTO.getGraininess());
                                //我这个 listTree 已经是分好组的 数据 就是要遍历 这个 listTree 找出父子级关系 然后 比对时间  如果是父子级id 就比较
                                listTree.add(powerVo);
                            }

                        } else if (energyDTO.getGraininess().equals("5")) {
                            collect = resultList.stream().
                                    collect(Collectors.groupingBy(HalfHourPowerMeterSummaryEntity::getYearStr));

                            for (Map.Entry<String, List<HalfHourPowerMeterSummaryEntity>> entry : collect.entrySet()) {

                                List<HalfHourPowerMeterSummaryEntity> value = entry.getValue();
                                //树的根节点  第一层的所有时间段数据  一个时间段就代表一个树的根节点
                                HashMap<String, Object> mapWyf = new HashMap<>();
                                HashMap<String, Object> mapCxk = new HashMap<>();

                                HashMap<String, Object> mapLh = new HashMap<>();
                                HashMap<String, Object> mapYy = new HashMap<>();

                                //峰
                                HashMap<String, Object> mapPeekValue = new HashMap<>();
                                HashMap<String, Object> mapPeekValues = new HashMap<>();
                                //平
                                HashMap<String, Object> mapFlatValue = new HashMap<>();
                                HashMap<String, Object> mapFlatValues = new HashMap<>();
                                //谷
                                HashMap<String, Object> mapValleyValue = new HashMap<>();
                                HashMap<String, Object> mapValleyValues = new HashMap<>();
                                PowerVo powerVo = new PowerVo();
                                String time = "";
                                String rateType = "";

                                //把设备id 对应的能耗放进map  把时间和类型放到对象里
                                for (HalfHourPowerMeterSummaryEntity hs : value) {
                                    //费用
                                    mapLh.put(hs.getMeterId(), hs.getTotalPrice());
                                    //能耗
                                    mapWyf.put(hs.getMeterId(), hs.getDetActiveEnergy());
                                    //峰
                                    mapPeekValue.put(hs.getMeterId(), hs.getPeekValue());
                                    //平
                                    mapFlatValue.put(hs.getMeterId(), hs.getFlatValue());
                                    //谷
                                    mapValleyValue.put(hs.getMeterId(), hs.getValleyValue());
                                    time = hs.getYearStr();
                                    rateType = hs.getRateType();
                                    hashSet.add(hs.getYearStr());

                                }
                                //把设备id拼接ll 对应能耗放进对应id里 有设备id的 没有能耗的放0
                                //[DHU0011,DHU0012]
                                //map{"DHU0012","123321"}
                                for (Object ls : list) {
                                    for (Map.Entry<String, Object> stringObjectEntry : mapWyf.entrySet()) {
                                        if (!ls.equals(stringObjectEntry.getKey())) {
                                            mapCxk.put("LL" + ls, 0);

                                        }
                                        if (ls.equals(stringObjectEntry.getKey())) {
                                            mapCxk.put("LL" + ls, stringObjectEntry.getValue());
                                            break;
                                        }
                                    }
                                }

                                //把设备id拼接ll 对应费率放进对应id里 有设备id的 没有费率的放0
                                //[DHU0011,DHU0012]
                                //map{"DHU0012","123321"}
                                for (Object ls : list) {
                                    for (Map.Entry<String, Object> stringObjectEntry : mapLh.entrySet()) {
                                        if (!ls.equals(stringObjectEntry.getKey())) {
                                            mapYy.put("LL" + ls, 0);

                                        }
                                        if (ls.equals(stringObjectEntry.getKey())) {
                                            mapYy.put("LL" + ls, stringObjectEntry.getValue());
                                            break;
                                        }
                                    }
                                }

                                //把设备id拼接ll 对应费率放进对应id里 有设备id的 没有费率的放0
                                //[DHU0011,DHU0012]
                                //map{"DHU0012","123321"}
                                //峰
                                for (Object ls : list) {
                                    for (Map.Entry<String, Object> stringObjectEntry : mapPeekValue.entrySet()) {
                                        if (!ls.equals(stringObjectEntry.getKey())) {
                                            mapPeekValues.put("LL" + ls, 0);

                                        }
                                        if (ls.equals(stringObjectEntry.getKey())) {
                                            mapPeekValues.put("LL" + ls, stringObjectEntry.getValue());
                                            break;
                                        }
                                    }
                                }
                                //把设备id拼接ll 对应费率放进对应id里 有设备id的 没有费率的放0
                                //[DHU0011,DHU0012]
                                //map{"DHU0012","123321"}
                                //平
                                for (Object ls : list) {
                                    for (Map.Entry<String, Object> stringObjectEntry : mapFlatValue.entrySet()) {
                                        if (!ls.equals(stringObjectEntry.getKey())) {
                                            mapFlatValues.put("LL" + ls, 0);

                                        }
                                        if (ls.equals(stringObjectEntry.getKey())) {
                                            mapFlatValues.put("LL" + ls, stringObjectEntry.getValue());
                                            break;
                                        }
                                    }
                                }

                                //把设备id拼接ll 对应费率放进对应id里 有设备id的 没有费率的放0
                                //[DHU0011,DHU0012]
                                //map{"DHU0012","123321"}
                                //谷
                                for (Object ls : list) {
                                    for (Map.Entry<String, Object> stringObjectEntry : mapValleyValue.entrySet()) {
                                        if (!ls.equals(stringObjectEntry.getKey())) {
                                            mapValleyValues.put("LL" + ls, 0);

                                        }
                                        if (ls.equals(stringObjectEntry.getKey())) {
                                            mapValleyValues.put("LL" + ls, stringObjectEntry.getValue());
                                            break;
                                        }
                                    }
                                }


                                //{"llDHU0012+llDHU0011",{LLDHU0012:123},{LLDHU0011:0}}
                                //把上面的公式 和 map放进方法 算出能耗值
                                Float num = Float.parseFloat(ExpressionUtil.eval(s, mapCxk).toString());
                                //费率
                                Float price = Float.parseFloat(ExpressionUtil.eval(s, mapYy).toString());

                                //峰
                                Float PeekValue = Float.parseFloat(ExpressionUtil.eval(s, mapPeekValues).toString());
                                //平
                                Float FlatValue = Float.parseFloat(ExpressionUtil.eval(s, mapFlatValues).toString());
                                //谷
                                Float ValleyValue = Float.parseFloat(ExpressionUtil.eval(s, mapValleyValues).toString());
                                //平均价
                                Float rate = 0.00F;
                                if (num > 0 && price > 0) {
                                    rate = (price / num);
                                }
                                //保留小数点两位，采用截断模式
                                num = new BigDecimal(num).setScale(2, RoundingMode.DOWN).floatValue();
                                PeekValue = new BigDecimal(PeekValue).setScale(2, RoundingMode.DOWN).floatValue();
                                FlatValue = new BigDecimal(FlatValue).setScale(2, RoundingMode.DOWN).floatValue();
                                ValleyValue = new BigDecimal(ValleyValue).setScale(2, RoundingMode.DOWN).floatValue();
                                price = new BigDecimal(price).setScale(2, RoundingMode.DOWN).floatValue();
                                rate = new BigDecimal(rate).setScale(2, RoundingMode.DOWN).floatValue();
                                //小数点不足两位添0
                                String numStr = decimalFormat.format(num);
                                String rates = decimalFormat.format(rate);
                                String prices = decimalFormat.format(price);
                                String ValleyValues = decimalFormat.format(ValleyValue);
                                String FlatValues = decimalFormat.format(FlatValue);
                                String PeekValues = decimalFormat.format(PeekValue);
                                //把所有需要的值全放入powerVo这个对象

                                powerVo.setRate(rates);
                                powerVo.setNum(numStr);
                                powerVo.setPrice(prices);
                                powerVo.setRateType(rateType);
                                powerVo.setId(id);
                                powerVo.setParentId(parentId);
                                powerVo.setName(name);
                                powerVo.setTime(time);
                                powerVo.setPeekValue(PeekValues);
                                powerVo.setFlatValue(FlatValues);
                                powerVo.setValleyValue(ValleyValues);
                                powerVo.setGraininess(energyDTO.getGraininess());
                                //我这个 listTree 已经是分好组的 数据 就是要遍历 这个 listTree 找出父子级关系 然后 比对时间  如果是父子级id 就比较
                                listTree.add(powerVo);
                            }

                        }
                    } else {
                        PowerVo powerVo = new PowerVo();
                        //把判断颗粒度时没有 计算公式的放到这个对象集合里
                        powerVo.setId(id);
                        powerVo.setParentId(parentId);
                        powerVo.setName(name);
                        powerVo.setGraininess(energyDTO.getGraininess());
                        //我这个 listTree 已经是分好组的 数据 就是要遍历 这个 listTree 找出父子级关系 然后 比对时间  如果是父子级id 就比较
                        listPower.add(powerVo);
                    }
                    if (listPower.size() > 0) {
                        for (PowerVo powerVo : listPower) {
                            for (String setTime : hashSet) {
                                PowerVo powerVo1 = new PowerVo();
                                powerVo1.setId(powerVo.getId());
                                powerVo1.setParentId(powerVo.getParentId());
                                powerVo1.setGraininess(powerVo.getGraininess());
                                powerVo1.setRateType("");
                                powerVo1.setName(powerVo.getName());
                                powerVo1.setTime(setTime);
                                listTree.add(powerVo1);
                            }
                        }
                    }

                }
                Map<String, List<PowerVo>> collectPower = listTree.stream().collect(Collectors.groupingBy(PowerVo::getTime));
                PowerMeterSummaryUtils ps = new PowerMeterSummaryUtils();
                List<Map<String, Object>> time = ps.PowerTree(collectPower, energyDTO);
                if (energyDTO.getGraininess().equals("2") || energyDTO.getGraininess().equals("3")) {
                    lisAllTree = time.stream().sorted(Comparator
                                    .comparing((Map<String, Object> map) ->
                                            new DateTime(map.get("time").toString(), "yyyy-MM-dd")).reversed())
                            .collect(Collectors.toList());
                } else if (energyDTO.getGraininess().equals("4")) {
                    lisAllTree = time.stream().sorted(Comparator
                                    .comparing((Map<String, Object> map) ->
                                            new DateTime(map.get("time").toString(), "yyyy-MM")).reversed())
                            .collect(Collectors.toList());
                } else if (energyDTO.getGraininess().equals("5")) {
                    lisAllTree = time.stream().sorted(Comparator
                                    .comparing((Map<String, Object> map) ->
                                            new DateTime(map.get("time").toString(), "yyyy")).reversed())
                            .collect(Collectors.toList());
                }
            }

            List<String> deviceIdList = new ArrayList(deviceIdSet);
            energyDTO1.setIds(deviceIdList);
            lisAllTreeFinal.addAll(lisAllTree);
            if (ChronoUnit.HOURS.between(startTimeInitial.toInstant(),
                    DateUtil.parse(energyDTO.getStartTime(), getDateFormat(energyDTO.getStartTime())).toInstant()) == 0) {
                break;
            }
//            为了快速找到开始查询的首次目标时间
            if (flag == true) {
                String offsetEndTime = powerMeterSummaryMapper.queryOffSetEndTime(energyDTO1, off);
                offsetEndTime = offsetEndTime == null ? startTimeInitial.toString() : offsetEndTime;
                if (offsetEndTime.length() < dateTimeEnd.toString().length()) {
                    offsetEndTime += dateTimeEnd.toString().substring(offsetEndTime.length());
                }
                if ("1".equals(energyDTO1.getGraininess())) {
                    format = "yyyy-MM-dd HH:mm";
                } else {
                    format = "yyyy-MM-dd";
                }
                if (!dateTimeEnd.toString().equals(offsetEndTime) && dateTimeEnd.toString().compareTo(offsetEndTime.toString()) >= 0) {
                    dateTimeEnd = DateUtil.parse(offsetEndTime, format);
                    lisAllTreeFinal.clear();
                } else {
                    dateTimeEnd = dateTimeStart;
                }
                flag = false;
            } else if (ChronoUnit.HOURS.between(dateTimeStart.toInstant(), dateTimeEnd.toInstant()) > 0) {
                dateTimeEnd = dateTimeStart;
            } else {
                break;
            }
            //计数，当lisAllTree连续5次为空就进一次时间范围缩小
            if (checkCount == 4 && lisAllTree.size() == 0) {
                EnergyDTO energyDTO2 = new EnergyDTO();
                BeanUtils.copyProperties(energyDTO, energyDTO2);
                energyDTO2.setStartTime(energyDTO1.getStartTime());
                String offSetEndTime = powerMeterSummaryMapper.queryOffSetEndTime(energyDTO, 0);
                offSetEndTime = offSetEndTime == null ? startTimeInitial.toString() : offSetEndTime;
                if (offSetEndTime.length() < dateTimeEnd.toString().length()) {
                    offSetEndTime += dateTimeEnd.toString().substring(offSetEndTime.length());
                }
                dateTimeEnd = DateUtil.parse(offSetEndTime, format);
                checkCount = 0;
            } else if (lisAllTree.size() == 0) {
                checkCount++;
            } else if (lisAllTree.size() > 0) {
                checkCount = 0;
            }
            switch (energyDTO1.getGraininess()) {
                case "1":
                    format = "yyyy-MM-dd HH:mm";
                    dateTimeStart = DateUtil.offset(dateTimeEnd, DateField.HOUR, -size / 2);
                    break;
                case "2":
                    format = "yyyy-MM-dd";
                    dateTimeStart = DateUtil.offset(dateTimeEnd, DateField.DAY_OF_WEEK, -size);
                    break;
                case "3":
                    format = "yyyy-MM-dd";
                    dateTimeStart = DateUtil.offset(dateTimeEnd, DateField.WEEK_OF_MONTH, -size);
                    break;
                case "4":
                    format = "yyyy-MM-dd";
                    dateTimeStart = DateUtil.offset(dateTimeEnd, DateField.MONTH, -size);
                    break;
                default:
                    dateTimeStart = DateUtil.offset(dateTimeEnd, DateField.YEAR, -size);
                    format = "yyyy-MM-dd";
                    break;
            }
            if (ChronoUnit.HOURS.between(startTimeInitial.toInstant(), dateTimeStart.toInstant()) >= 0) {
                energyDTO.setStartTime(DateUtil.format(dateTimeStart, format));
            } else {
                energyDTO.setStartTime(DateUtil.format(startTimeInitial, format));
            }
            energyDTO.setEndTime(DateUtil.format(dateTimeEnd, format));
        }

        if ("1".equals(energyDTO.getGraininess())) {
            total = powerMeterSummaryMapper.queryHourTotal(energyDTO1);
        } else {
            total = powerMeterSummaryMapper.queryDateTotal(energyDTO1);
        }
//        获取分页数
        long pages = (long) Math.ceil((double) total / size);
////        设置page属性
        int indexEnd = (int) (total - off);
        indexEnd = indexEnd <= pageDTO.getPage() ? indexEnd : size;
        indexEnd = indexEnd <= lisAllTreeFinal.size() ? indexEnd : lisAllTreeFinal.size();
        page.setRecords(lisAllTreeFinal.subList(0, indexEnd));
        page.setTotal(total);
        page.setCurrent(pageDTO.getPage());
        page.setPages(pages);
        page.setSize(pageDTO.getSize());
        return page;
    }

    /**
     * 环比
     *
     * @param
     * @return
     */
    @Override
    public List<HalfHourPowerMeterSummaryEntityVo> selectLinkRelativeRatio() {
        HalfHourPowerMeterSummaryEntityVo hsr = new HalfHourPowerMeterSummaryEntityVo();
        EnergyDTO energyDTO = new EnergyDTO();
        List<HalfHourPowerMeterSummaryEntityVo> list = new ArrayList<>();
        DateTime dateTime = new DateTime();
        String substringEnd = dateTime.offset(DateField.MONTH, -1).toString().substring(0, 7);

        DateTime dateTime1 = new DateTime();
        String substringStart = dateTime1.offset(DateField.MONTH, -2).toString().substring(0, 7);

        energyDTO.setStartTime(substringStart);
        energyDTO.setEndTime(substringEnd);

        energyDTO.setRateType("电");
        List<HalfHourPowerMeterSummaryEntityVo> hs = powerMeterSummaryMapper.selectLinkRelativeRatio(energyDTO);
        if (hs.size() > 0) {
            Optional<Float> reduce = hs.stream().map(HalfHourPowerMeterSummaryEntityVo::getTotalPrice).reduce(Float::sum);

            Optional<Float> reduce2 = hs.stream().map(HalfHourPowerMeterSummaryEntityVo::getDetActiveEnergy).reduce(Float::sum);

            hsr.setEndTime(substringEnd);
            hsr.setLinkrelativeratio(reduce.get());
            hsr.setActivelinkrelativeratio(reduce2.get());
            list.add(hsr);
        }else{
            hsr.setEndTime(substringEnd);
            hsr.setLinkrelativeratio(0);
            hsr.setActivelinkrelativeratio(0);
            list.add(hsr);
        }
        HalfHourPowerMeterSummaryEntityVo hsreo = new HalfHourPowerMeterSummaryEntityVo();
        EnergyDTO energyDTO2 = new EnergyDTO();
        DateTime dateTimeIndex = new DateTime();
        String substringEnd2 = dateTimeIndex.offset(DateField.MONTH, -2).toString().substring(0, 7);

        DateTime dateTimeIndex1 = new DateTime();
        String substringStart2 = dateTimeIndex1.offset(DateField.MONTH, -3).toString().substring(0, 7);

        energyDTO2.setStartTime(substringStart2);
        energyDTO2.setEndTime(substringEnd2);

        energyDTO2.setRateType("电");
        List<HalfHourPowerMeterSummaryEntityVo> hsm = powerMeterSummaryMapper.selectLinkRelativeRatio(energyDTO2);
        if (hsm.size() > 0) {
            Optional<Float> reduces = hsm.stream().map(HalfHourPowerMeterSummaryEntityVo::getTotalPrice).reduce(Float::sum);
            Optional<Float> reduces2 = hsm.stream().map(HalfHourPowerMeterSummaryEntityVo::getDetActiveEnergy).reduce(Float::sum);
            hsreo.setEndTime(substringEnd2);
            hsreo.setLinkrelativeratio(reduces.get());
            hsreo.setActivelinkrelativeratio(reduces2.get());
            list.add(hsreo);
        }else{
            hsreo.setEndTime(substringEnd2);
            hsreo.setLinkrelativeratio(0);
            hsreo.setActivelinkrelativeratio(0);
            list.add(hsreo);
        }
        return list;
    }

    /**
     * 同比
     */
    @Override
    public List<HalfHourPowerMeterSummaryEntityVo> selectYearBasis() {
        HalfHourPowerMeterSummaryEntityVo hsr = new HalfHourPowerMeterSummaryEntityVo();
        EnergyDTO energyDTO = new EnergyDTO();
        List<HalfHourPowerMeterSummaryEntityVo> list = new ArrayList<>();

        //同比 取去年 2021-8月份
        DateTime dateTime = new DateTime();
        dateTime.offset(DateField.MONTH, -1).toString().substring(0, 7);
        String substringEnd = dateTime.offset(DateField.YEAR, -1).toString().substring(0, 7);

        //同比 取去年 2021-7月份
        DateTime dateTime1 = new DateTime();
        dateTime1.offset(DateField.MONTH, -2).toString().substring(0, 7);
        String substringStart = dateTime1.offset(DateField.YEAR, -1).toString().substring(0, 7);


        energyDTO.setStartTime(substringStart);
        energyDTO.setEndTime(substringEnd);


        energyDTO.setRateType("电");
        List<HalfHourPowerMeterSummaryEntityVo> hs = powerMeterSummaryMapper.selectLinkRelativeRatio(energyDTO);
        if (hs.size() > 0) {
            Optional<Float> reduce = hs.stream().map(HalfHourPowerMeterSummaryEntityVo::getTotalPrice).reduce(Float::sum);

            Optional<Float> reduce2 = hs.stream().map(HalfHourPowerMeterSummaryEntityVo::getDetActiveEnergy).reduce(Float::sum);

            hsr.setEndTime(substringEnd);
            hsr.setLinkrelativeratio(reduce.get());
            hsr.setActivelinkrelativeratio(reduce2.get());
            list.add(hsr);
        }else{
            hsr.setEndTime(substringEnd);
            hsr.setLinkrelativeratio(0);

            hsr.setActivelinkrelativeratio(0);
            list.add(hsr);
        }
        HalfHourPowerMeterSummaryEntityVo hsreo = new HalfHourPowerMeterSummaryEntityVo();
        EnergyDTO energyDTO2 = new EnergyDTO();

        //同比 取今年 2022-8月份
        DateTime dateTimeIndex = new DateTime();
        String substringEnd2 = dateTimeIndex.offset(DateField.MONTH, -1).toString().substring(0, 7);

        //同比 取今年 2022-7月份
        DateTime dateTimeIndex1 = new DateTime();
        String substringStart2 = dateTimeIndex1.offset(DateField.MONTH, -2).toString().substring(0, 7);

        energyDTO2.setStartTime(substringStart2);
        energyDTO2.setEndTime(substringEnd2);

        energyDTO2.setRateType("电");
        List<HalfHourPowerMeterSummaryEntityVo> hsm = powerMeterSummaryMapper.selectLinkRelativeRatio(energyDTO2);
        if (hsm.size() > 0) {
            Optional<Float> reduces = hsm.stream().map(HalfHourPowerMeterSummaryEntityVo::getTotalPrice).reduce(Float::sum);
            Optional<Float> reduces2 = hsm.stream().map(HalfHourPowerMeterSummaryEntityVo::getDetActiveEnergy).reduce(Float::sum);

            hsreo.setEndTime(substringEnd2);
            hsreo.setLinkrelativeratio(reduces.get());
            hsreo.setActivelinkrelativeratio(reduces2.get());
            list.add(hsreo);
        }else{
            hsreo.setEndTime(substringEnd2);
            hsreo.setLinkrelativeratio(0);
            hsreo.setActivelinkrelativeratio(0);
            list.add(hsreo);
        }

        return list;
    }


    /**
     * 碳足迹
     */
    @Override
    public List<Map<String, Object>> selectCarbonFootprint(EnergyDTO energyDTO) {
        //                        设置小数点不足两位添0格式
        DecimalFormat decimalFormat = new DecimalFormat(".00");
        List<Map<String, Object>> lisAllTree = new ArrayList<>();
        //遍历 拿到设备 电的计算规则
        List<Map<String, String>> stringObjMoudelMap = objMoudelService.selectMetering(energyDTO);
        List<PowerVo> listTree = new ArrayList<>();
        List<PowerVo> listPower = new ArrayList<>();

        HashSet<String> hashSet = new HashSet();

        List<HalfHourPowerMeterSummaryEntity> hfList = powerMeterSummaryMapper.selectDatePrice(energyDTO);
        if (hfList.size() > 0) {
            Map<String, List<HalfHourPowerMeterSummaryEntity>> hfMap = hfList.stream().collect(Collectors.groupingBy(HalfHourPowerMeterSummaryEntity::getMeterId));

            for (Map map : stringObjMoudelMap) {
                String id = (String) map.get("id");
                String parentId = (String) map.get("parentId");
                String name = (String) map.get("name");
                //拿到为电的计算公式
                if (map.get("电") != null && !"".equals(map.get("电"))) {
                    String electricity = (String) map.get("电");
                    energyDTO.setRateType("电");
                    //格式化计算公式
                    String s = MyExpressionUtil.quChu(electricity);
                    //拿到计算公式里的所有deviceId

                    List<String> expressionParams = MyExpressionUtil.getExpressionParams(electricity);
                    //循环拿到deviceId
                    List list = new ArrayList();
                    for (String index : expressionParams) {
                        list.add(index);
                    }
                    //energyDTO.setIds(list);

                    List<HalfHourPowerMeterSummaryEntity> resultList = new ArrayList<>(8);
                    expressionParams.forEach(e -> {
                        List<HalfHourPowerMeterSummaryEntity> deviceDataList = hfMap.get(e);
                        if (CollectionUtil.isNotEmpty(deviceDataList)) {
                            resultList.addAll(deviceDataList);
                        }
                    });


//                    collect = resultList.stream().
//                            collect(Collectors.groupingBy(HalfHourPowerMeterSummaryEntity::getDayStr));

                    //查出来所有数据 按时间进行分组
                    Map<String, List<HalfHourPowerMeterSummaryEntity>> collect = resultList.stream().
                            collect(Collectors.groupingBy(HalfHourPowerMeterSummaryEntity::getDayStr));

                    for (Map.Entry<String, List<HalfHourPowerMeterSummaryEntity>> entry : collect.entrySet()) {

                        List<HalfHourPowerMeterSummaryEntity> value = entry.getValue();
                        //树的根节点  第一层的所有时间段数据  一个时间段就代表一个树的根节点
                        HashMap<String, Object> mapWyf = new HashMap<>();
                        HashMap<String, Object> mapCxk = new HashMap<>();

                        HashMap<String, Object> mapLh = new HashMap<>();
                        HashMap<String, Object> mapYy = new HashMap<>();


                        PowerVo powerVo = new PowerVo();
                        String time = "";
                        String rateType = "";

                        //把设备id 对应的能耗放进map  把时间和类型放到对象里
                        for (HalfHourPowerMeterSummaryEntity hs : value) {
                            //费用
                            mapLh.put(hs.getMeterId(), hs.getTotalPrice());
                            //能耗
                            mapWyf.put(hs.getMeterId(), hs.getDetActiveEnergy());

                            time = hs.getDayStr();
                            rateType = hs.getRateType();
                            hashSet.add(hs.getDayStr());

                        }
                        //把设备id拼接ll 对应能耗放进对应id里 有设备id的 没有能耗的放0
                        //[DHU0011,DHU0012]
                        //map{"DHU0012","123321"}
                        for (Object ls : list) {
                            for (Map.Entry<String, Object> stringObjectEntry : mapWyf.entrySet()) {
                                if (!ls.equals(stringObjectEntry.getKey())) {
                                    mapCxk.put("LL" + ls, 0);

                                }
                                if (ls.equals(stringObjectEntry.getKey())) {
                                    mapCxk.put("LL" + ls, stringObjectEntry.getValue());
                                    break;
                                }
                            }
                        }

                        //把设备id拼接ll 对应费率放进对应id里 有设备id的 没有费率的放0
                        //[DHU0011,DHU0012]
                        //map{"DHU0012","123321"}
                        for (Object ls : list) {
                            for (Map.Entry<String, Object> stringObjectEntry : mapLh.entrySet()) {
                                if (!ls.equals(stringObjectEntry.getKey())) {
                                    mapYy.put("LL" + ls, 0);

                                }
                                if (ls.equals(stringObjectEntry.getKey())) {
                                    mapYy.put("LL" + ls, stringObjectEntry.getValue());
                                    break;
                                }
                            }
                        }

                        //{"llDHU0012+llDHU0011",{LLDHU0012:123},{LLDHU0011:0}}
                        //把上面的公式 和 map放进方法 算出能耗值
                        Float num = Float.parseFloat(ExpressionUtil.eval(s, mapCxk).toString());
                        //费率
                        Float price = Float.parseFloat(ExpressionUtil.eval(s, mapYy).toString());

                        //平均价
                        Float rate = 0.00F;
                        if (num > 0 && price > 0) {
                            rate = (price / num);
                        }
                        // 碳足迹
                        double carbon = 0.00F;
                        if (num > 0) {
                            carbon = num * 0.785;
                        }
                        //保留小数点两位，采用截断模式
                        num = new BigDecimal(num).setScale(2, RoundingMode.DOWN).floatValue();
                        price = new BigDecimal(price).setScale(2, RoundingMode.DOWN).floatValue();
                        rate = new BigDecimal(rate).setScale(2, RoundingMode.DOWN).floatValue();

                        //小数点不足两位添0
                        String numStr = decimalFormat.format(num);
                        String rates = decimalFormat.format(rate);
                        String prices = decimalFormat.format(price);
                        //把所有需要的值全放入powerVo这个对象
                        //碳足迹
                        powerVo.setCarbon(carbon);
                        //把所有需要的值全放入powerVo这个对象
                        powerVo.setRate(rates);
                        powerVo.setNum(numStr);
                        powerVo.setPrice(prices);
                        powerVo.setRateType(rateType);
                        powerVo.setId(id);
                        powerVo.setParentId(parentId);
                        powerVo.setName(name);
                        powerVo.setTime(time);
                        powerVo.setGraininess(energyDTO.getGraininess());

                        //我这个 listTree 已经是分好组的 数据 就是要遍历 这个 listTree 找出父子级关系 然后 比对时间  如果是父子级id 就比较
                        listTree.add(powerVo);
                    }
                } else {
                    PowerVo powerVo = new PowerVo();
                    //把判断颗粒度时没有 计算公式的放到这个对象集合里
                    powerVo.setId(id);
                    powerVo.setParentId(parentId);
                    powerVo.setName(name);

                    powerVo.setGraininess(energyDTO.getGraininess());
                    //我这个 listTree 已经是分好组的 数据 就是要遍历 这个 listTree 找出父子级关系 然后 比对时间  如果是父子级id 就比较
                    listPower.add(powerVo);
                }
                if (listPower.size() > 0) {
                    for (PowerVo powerVo : listPower) {
                        for (String setTime : hashSet) {
                            PowerVo powerVo1 = new PowerVo();
                            powerVo1.setId(powerVo.getId());
                            powerVo1.setParentId(powerVo.getParentId());
                            powerVo1.setGraininess(powerVo.getGraininess());
                            powerVo1.setRateType("");
                            powerVo1.setCarbon(0.00);
                            powerVo1.setName(powerVo.getName());
                            powerVo1.setTime(setTime);
                            listTree.add(powerVo1);
                        }
                    }
                }
            }


            Map<String, Double> collect = listTree.stream().collect(Collectors.groupingBy(PowerVo::getId, Collectors.summingDouble(PowerVo::getCarbon)));
            PowerMeterSummaryUtils ps = new PowerMeterSummaryUtils();
            lisAllTree = ps.CrbonPwerTree(collect, energyDTO);
            return lisAllTree;
        }
        return null;
    }


    /**
     * 常规自动日期格式识别  返回对应的时间格式  yyyy-MM-dd   yyyy-MM-dd HH:mm:ss
     *
     * @param str 时间字符串
     * @return Date
     * @author dc
     */
    private String getDateFormat(String str) {
        boolean year = false;
        Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");
        if (pattern.matcher(str.substring(0, 4)).matches()) {
            year = true;
        }
        StringBuilder sb = new StringBuilder();
        int index = 0;
        if (!year) {
            if (str.contains("月") || str.contains("-") || str.contains("/")) {
                if (Character.isDigit(str.charAt(0))) {
                    index = 1;
                }
            } else {
                index = 3;
            }
        }
        for (int i = 0; i < str.length(); i++) {
            char chr = str.charAt(i);
            if (Character.isDigit(chr)) {
                if (index == 0) {
                    sb.append("y");
                }
                if (index == 1) {
                    sb.append("M");
                }
                if (index == 2) {
                    sb.append("d");
                }
                if (index == 3) {
                    sb.append("H");
                }
                if (index == 4) {
                    sb.append("m");
                }
                if (index == 5) {
                    sb.append("s");
                }

                if (index == 6) {
                    sb.append("S");
                }

            } else {
                if (i > 0) {
                    char lastChar = str.charAt(i - 1);
                    if (Character.isDigit(lastChar)) {
                        index++;
                    }
                }
                sb.append(chr);
            }
        }
        return sb.toString();
    }


    public List<PowerVo> selectHourPriceExcel(EnergyDTO energyDTO) {

        List<PowerVo> listExcel = new ArrayList<>();
        Map<String, List<PowerVo>> collectPower = new HashMap<>();
        List<PowerVo> powerVoList = new ArrayList<>();
        deviceIdSet = new HashSet<>();
//        设置小数点不足两位添0格式
        DecimalFormat decimalFormat = new DecimalFormat("0.00");
        if (energyDTO.getGraininess().equals("1")) {
            //遍历 拿到设备 电的计算规则
            List<PowerVo> listTree = pageExcel(energyDTO);
            listTree.forEach(powerVo -> {
                if (powerVo.getId().equals(energyDTO.getAnalysisObject()) || powerVo.getParentId().equals(energyDTO.getAnalysisObject())) {
                    listExcel.add(powerVo);
                }
            });


            //把 listTree 按世间分组

            collectPower = listExcel.stream().collect(Collectors.groupingBy(PowerVo::getTime));
            collectPower.entrySet().stream().sorted(Comparator.comparing(Map.Entry::getKey))
                    .forEach(e -> {
                        Map<String, Object> map = new HashMap<>();
                        map.put(e.getKey(), e.getValue());
                        List<PowerVo> powerVos = e.getValue().stream().map(item -> {
                            PowerVo powerVo = new PowerVo();
                            BeanUtils.copyProperties(item, powerVo);
                            return powerVo;
                        }).collect(Collectors.toList());
                        powerVoList.addAll(powerVos);
                    });
        } else {
            int k=0;
            //遍历 拿到设备 电的计算规则
            List<Map<String, String>> stringObjMoudelMap = objMoudelService.selectMetering(energyDTO);
            List<PowerVo> listTree = new ArrayList<>();
//            List<PowerVo> listPower = new ArrayList<>();
            Map<Integer, PowerVo> mapPower = new HashMap<>();
            HashSet<String> hashSet = new HashSet();

            HashSet<String> hashSet1 = new HashSet();

            HashSet<String> hashSet2 = new HashSet();

            HashSet<String> hashSet3 = new HashSet();

            List<HalfHourPowerMeterSummaryEntity> hfList = powerMeterSummaryMapper.selectDatePricePage(energyDTO);
            Map<String, List<HalfHourPowerMeterSummaryEntity>> hfMap = hfList.stream().collect(Collectors.groupingBy(HalfHourPowerMeterSummaryEntity::getMeterId));
            for (HalfHourPowerMeterSummaryEntity hs : hfList) {
                hashSet.add(hs.getDayStr());
                hashSet1.add(hs.getWeekStr());
                hashSet2.add(hs.getMonthStr());
                hashSet3.add(hs.getYearStr());
            }
            for (Map map : stringObjMoudelMap) {
                String id = (String) map.get("id");
                String parentId = (String) map.get("parentId");
                String name = (String) map.get("name");
                //拿到为电的计算公式
                if (map.get("电") != null && !"".equals(map.get("电"))) {
                    String electricity = (String) map.get("电");
                    energyDTO.setRateType("电");
                    //格式化计算公式
                    String s = MyExpressionUtil.quChu(electricity);
                    //拿到计算公式里的所有deviceId

                    List<String> expressionParams = MyExpressionUtil.getExpressionParams(electricity);
                    //循环拿到deviceId
                    List list = new ArrayList();
                    for (String index : expressionParams) {
                        list.add(index);
                        deviceIdSet.add(index);
                    }
                    //energyDTO.setIds(list);
                    List<HalfHourPowerMeterSummaryEntity> resultList = new ArrayList<>(8);
                    expressionParams.forEach(e -> {
                        List<HalfHourPowerMeterSummaryEntity> deviceDataList = hfMap.get(e);
                        if (CollectionUtil.isNotEmpty(deviceDataList)) {
                            resultList.addAll(deviceDataList);
                        }
                    });

                    Map<String, List<HalfHourPowerMeterSummaryEntity>> collect = new HashMap<>();
                    if (energyDTO.getGraininess().equals("2")) {
                        collect = resultList.stream().
                                collect(Collectors.groupingBy(HalfHourPowerMeterSummaryEntity::getDayStr));
                        for (Map.Entry<String, List<HalfHourPowerMeterSummaryEntity>> entry : collect.entrySet()) {

                            List<HalfHourPowerMeterSummaryEntity> value = entry.getValue();
                            //树的根节点  第一层的所有时间段数据  一个时间段就代表一个树的根节点
                            HashMap<String, Object> mapWyf = new HashMap<>();
                            HashMap<String, Object> mapCxk = new HashMap<>();

                            HashMap<String, Object> mapLh = new HashMap<>();
                            HashMap<String, Object> mapYy = new HashMap<>();

                            //峰
                            HashMap<String, Object> mapPeekValue = new HashMap<>();
                            HashMap<String, Object> mapPeekValues = new HashMap<>();
                            //平
                            HashMap<String, Object> mapFlatValue = new HashMap<>();
                            HashMap<String, Object> mapFlatValues = new HashMap<>();
                            //谷
                            HashMap<String, Object> mapValleyValue = new HashMap<>();
                            HashMap<String, Object> mapValleyValues = new HashMap<>();

                            PowerVo powerVo = new PowerVo();
                            String time = "";
                            String rateType = "";

                            //把设备id 对应的能耗放进map  把时间和类型放到对象里
                            for (HalfHourPowerMeterSummaryEntity hs : value) {
                                //费用
                                mapLh.put(hs.getMeterId(), hs.getTotalPrice());
                                //能耗
                                mapWyf.put(hs.getMeterId(), hs.getDetActiveEnergy());
                                //峰
                                mapPeekValue.put(hs.getMeterId(), hs.getPeekValue());
                                //平
                                mapFlatValue.put(hs.getMeterId(), hs.getFlatValue());
                                //谷
                                mapValleyValue.put(hs.getMeterId(), hs.getValleyValue());


                                time = hs.getDayStr();
                                rateType = hs.getRateType();
//                                hashSet.add(hs.getDayStr());

                            }
                            //把设备id拼接ll 对应能耗放进对应id里 有设备id的 没有能耗的放0
                            //[DHU0011,DHU0012]
                            //map{"DHU0012","123321"}
                            for (Object ls : list) {
                                for (Map.Entry<String, Object> stringObjectEntry : mapWyf.entrySet()) {
                                    if (!ls.equals(stringObjectEntry.getKey())) {
                                        mapCxk.put("LL" + ls, 0);

                                    }
                                    if (ls.equals(stringObjectEntry.getKey())) {
                                        mapCxk.put("LL" + ls, stringObjectEntry.getValue());
                                        break;
                                    }
                                }
                            }

                            //把设备id拼接ll 对应费率放进对应id里 有设备id的 没有费率的放0
                            //[DHU0011,DHU0012]
                            //map{"DHU0012","123321"}
                            for (Object ls : list) {
                                for (Map.Entry<String, Object> stringObjectEntry : mapLh.entrySet()) {
                                    if (!ls.equals(stringObjectEntry.getKey())) {
                                        mapYy.put("LL" + ls, 0);

                                    }
                                    if (ls.equals(stringObjectEntry.getKey())) {
                                        mapYy.put("LL" + ls, stringObjectEntry.getValue());
                                        break;
                                    }
                                }
                            }

                            //把设备id拼接ll 对应费率放进对应id里 有设备id的 没有费率的放0
                            //[DHU0011,DHU0012]
                            //map{"DHU0012","123321"}
                            //峰
                            for (Object ls : list) {
                                for (Map.Entry<String, Object> stringObjectEntry : mapPeekValue.entrySet()) {
                                    if (!ls.equals(stringObjectEntry.getKey())) {
                                        mapPeekValues.put("LL" + ls, 0);

                                    }
                                    if (ls.equals(stringObjectEntry.getKey())) {
                                        mapPeekValues.put("LL" + ls, stringObjectEntry.getValue());
                                        break;
                                    }
                                }
                            }
                            //把设备id拼接ll 对应费率放进对应id里 有设备id的 没有费率的放0
                            //[DHU0011,DHU0012]
                            //map{"DHU0012","123321"}
                            //平
                            for (Object ls : list) {
                                for (Map.Entry<String, Object> stringObjectEntry : mapFlatValue.entrySet()) {
                                    if (!ls.equals(stringObjectEntry.getKey())) {
                                        mapFlatValues.put("LL" + ls, 0);

                                    }
                                    if (ls.equals(stringObjectEntry.getKey())) {
                                        mapFlatValues.put("LL" + ls, stringObjectEntry.getValue());
                                        break;
                                    }
                                }
                            }

                            //把设备id拼接ll 对应费率放进对应id里 有设备id的 没有费率的放0
                            //[DHU0011,DHU0012]
                            //map{"DHU0012","123321"}
                            //谷
                            for (Object ls : list) {
                                for (Map.Entry<String, Object> stringObjectEntry : mapValleyValue.entrySet()) {
                                    if (!ls.equals(stringObjectEntry.getKey())) {
                                        mapValleyValues.put("LL" + ls, 0);

                                    }
                                    if (ls.equals(stringObjectEntry.getKey())) {
                                        mapValleyValues.put("LL" + ls, stringObjectEntry.getValue());
                                        break;
                                    }
                                }
                            }


                            //{"llDHU0012+llDHU0011",{LLDHU0012:123},{LLDHU0011:0}}
                            //把上面的公式 和 map放进方法 算出能耗值
                            Float num = Float.parseFloat(ExpressionUtil.eval(s, mapCxk).toString());
                            //费率
                            Float price = Float.parseFloat(ExpressionUtil.eval(s, mapYy).toString());

                            //峰
                            Float PeekValue = Float.parseFloat(ExpressionUtil.eval(s, mapPeekValues).toString());
                            //平
                            Float FlatValue = Float.parseFloat(ExpressionUtil.eval(s, mapFlatValues).toString());
                            //谷
                            Float ValleyValue = Float.parseFloat(ExpressionUtil.eval(s, mapValleyValues).toString());
                            //平均价
                            Float rate = 0.00F;
                            if (num > 0 && price > 0) {
                                rate = (price / num);
                            }
                            //保留小数点两位，采用截断模式
                            num = new BigDecimal(num).setScale(2, RoundingMode.DOWN).floatValue();
                            PeekValue = new BigDecimal(PeekValue).setScale(2, RoundingMode.DOWN).floatValue();
                            FlatValue = new BigDecimal(FlatValue).setScale(2, RoundingMode.DOWN).floatValue();
                            ValleyValue = new BigDecimal(ValleyValue).setScale(2, RoundingMode.DOWN).floatValue();
                            price = new BigDecimal(price).setScale(2, RoundingMode.DOWN).floatValue();
                            rate = new BigDecimal(rate).setScale(2, RoundingMode.DOWN).floatValue();
                            //小数点不足两位添0
                            String numStr = decimalFormat.format(num);
                            String rates = decimalFormat.format(rate);
                            String prices = decimalFormat.format(price);
                            String ValleyValues = decimalFormat.format(ValleyValue);
                            String FlatValues = decimalFormat.format(FlatValue);
                            String PeekValues = decimalFormat.format(PeekValue);
                            //把所有需要的值全放入powerVo这个对象
                            powerVo.setRate(rates);
                            powerVo.setNum(numStr);
                            powerVo.setPrice(prices);
                            powerVo.setRateType(rateType);
                            powerVo.setId(id);
                            powerVo.setParentId(parentId);
                            powerVo.setName(name);
                            powerVo.setTime(time);

                            powerVo.setPeekValue(PeekValues);
                            powerVo.setFlatValue(FlatValues);
                            powerVo.setValleyValue(ValleyValues);
                            powerVo.setGraininess(energyDTO.getGraininess());

                            //我这个 listTree 已经是分好组的 数据 就是要遍历 这个 listTree 找出父子级关系 然后 比对时间  如果是父子级id 就比较
                            listTree.add(powerVo);
                        }
                    } else if (energyDTO.getGraininess().equals("3")) {
                        collect = resultList.stream().
                                collect(Collectors.groupingBy(HalfHourPowerMeterSummaryEntity::getWeekStr));
                        for (Map.Entry<String, List<HalfHourPowerMeterSummaryEntity>> entry : collect.entrySet()) {

                            List<HalfHourPowerMeterSummaryEntity> value = entry.getValue();
                            //树的根节点  第一层的所有时间段数据  一个时间段就代表一个树的根节点
                            HashMap<String, Object> mapWyf = new HashMap<>();
                            HashMap<String, Object> mapCxk = new HashMap<>();

                            HashMap<String, Object> mapLh = new HashMap<>();
                            HashMap<String, Object> mapYy = new HashMap<>();

                            //峰
                            HashMap<String, Object> mapPeekValue = new HashMap<>();
                            HashMap<String, Object> mapPeekValues = new HashMap<>();
                            //平
                            HashMap<String, Object> mapFlatValue = new HashMap<>();
                            HashMap<String, Object> mapFlatValues = new HashMap<>();
                            //谷
                            HashMap<String, Object> mapValleyValue = new HashMap<>();
                            HashMap<String, Object> mapValleyValues = new HashMap<>();

                            PowerVo powerVo = new PowerVo();
                            String time = "";
                            String rateType = "";

                            //把设备id 对应的能耗放进map  把时间和类型放到对象里
                            for (HalfHourPowerMeterSummaryEntity hs : value) {
                                //费用
                                mapLh.put(hs.getMeterId(), hs.getTotalPrice());
                                //能耗
                                mapWyf.put(hs.getMeterId(), hs.getDetActiveEnergy());
                                //峰
                                mapPeekValue.put(hs.getMeterId(), hs.getPeekValue());
                                //平
                                mapFlatValue.put(hs.getMeterId(), hs.getFlatValue());
                                //谷
                                mapValleyValue.put(hs.getMeterId(), hs.getValleyValue());

                                time = hs.getWeekStr();
                                rateType = hs.getRateType();
//                                hashSet.add(hs.getWeekStr());

                            }
                            //把设备id拼接ll 对应能耗放进对应id里 有设备id的 没有能耗的放0
                            //[DHU0011,DHU0012]
                            //map{"DHU0012","123321"}
                            for (Object ls : list) {
                                for (Map.Entry<String, Object> stringObjectEntry : mapWyf.entrySet()) {
                                    if (!ls.equals(stringObjectEntry.getKey())) {
                                        mapCxk.put("LL" + ls, 0);

                                    }
                                    if (ls.equals(stringObjectEntry.getKey())) {
                                        mapCxk.put("LL" + ls, stringObjectEntry.getValue());
                                        break;
                                    }
                                }
                            }

                            //把设备id拼接ll 对应费率放进对应id里 有设备id的 没有费率的放0
                            //[DHU0011,DHU0012]
                            //map{"DHU0012","123321"}
                            for (Object ls : list) {
                                for (Map.Entry<String, Object> stringObjectEntry : mapLh.entrySet()) {
                                    if (!ls.equals(stringObjectEntry.getKey())) {
                                        mapYy.put("LL" + ls, 0);

                                    }
                                    if (ls.equals(stringObjectEntry.getKey())) {
                                        mapYy.put("LL" + ls, stringObjectEntry.getValue());
                                        break;
                                    }
                                }
                            }

                            //把设备id拼接ll 对应费率放进对应id里 有设备id的 没有费率的放0
                            //[DHU0011,DHU0012]
                            //map{"DHU0012","123321"}
                            //峰
                            for (Object ls : list) {
                                for (Map.Entry<String, Object> stringObjectEntry : mapPeekValue.entrySet()) {
                                    if (!ls.equals(stringObjectEntry.getKey())) {
                                        mapPeekValues.put("LL" + ls, 0);

                                    }
                                    if (ls.equals(stringObjectEntry.getKey())) {
                                        mapPeekValues.put("LL" + ls, stringObjectEntry.getValue());
                                        break;
                                    }
                                }
                            }
                            //把设备id拼接ll 对应费率放进对应id里 有设备id的 没有费率的放0
                            //[DHU0011,DHU0012]
                            //map{"DHU0012","123321"}
                            //平
                            for (Object ls : list) {
                                for (Map.Entry<String, Object> stringObjectEntry : mapFlatValue.entrySet()) {
                                    if (!ls.equals(stringObjectEntry.getKey())) {
                                        mapFlatValues.put("LL" + ls, 0);

                                    }
                                    if (ls.equals(stringObjectEntry.getKey())) {
                                        mapFlatValues.put("LL" + ls, stringObjectEntry.getValue());
                                        break;
                                    }
                                }
                            }

                            //把设备id拼接ll 对应费率放进对应id里 有设备id的 没有费率的放0
                            //[DHU0011,DHU0012]
                            //map{"DHU0012","123321"}
                            //谷
                            for (Object ls : list) {
                                for (Map.Entry<String, Object> stringObjectEntry : mapValleyValue.entrySet()) {
                                    if (!ls.equals(stringObjectEntry.getKey())) {
                                        mapValleyValues.put("LL" + ls, 0);

                                    }
                                    if (ls.equals(stringObjectEntry.getKey())) {
                                        mapValleyValues.put("LL" + ls, stringObjectEntry.getValue());
                                        break;
                                    }
                                }
                            }


                            //{"llDHU0012+llDHU0011",{LLDHU0012:123},{LLDHU0011:0}}
                            //把上面的公式 和 map放进方法 算出能耗值
                            Float num = Float.parseFloat(ExpressionUtil.eval(s, mapCxk).toString());
                            //费率
                            Float price = Float.parseFloat(ExpressionUtil.eval(s, mapYy).toString());

                            //峰
                            Float PeekValue = Float.parseFloat(ExpressionUtil.eval(s, mapPeekValues).toString());
                            //平
                            Float FlatValue = Float.parseFloat(ExpressionUtil.eval(s, mapFlatValues).toString());
                            //谷
                            Float ValleyValue = Float.parseFloat(ExpressionUtil.eval(s, mapValleyValues).toString());
                            //平均价
                            Float rate = 0.00F;
                            if (num > 0 && price > 0) {
                                rate = (price / num);
                            }
                            //保留小数点两位，采用截断模式
                            num = new BigDecimal(num).setScale(2, RoundingMode.DOWN).floatValue();
                            PeekValue = new BigDecimal(PeekValue).setScale(2, RoundingMode.DOWN).floatValue();
                            FlatValue = new BigDecimal(FlatValue).setScale(2, RoundingMode.DOWN).floatValue();
                            ValleyValue = new BigDecimal(ValleyValue).setScale(2, RoundingMode.DOWN).floatValue();
                            price = new BigDecimal(price).setScale(2, RoundingMode.DOWN).floatValue();
                            rate = new BigDecimal(rate).setScale(2, RoundingMode.DOWN).floatValue();
                            //小数点不足两位添0
                            String numStr = decimalFormat.format(num);
                            String rates = decimalFormat.format(rate);
                            String prices = decimalFormat.format(price);
                            String ValleyValues = decimalFormat.format(ValleyValue);
                            String FlatValues = decimalFormat.format(FlatValue);
                            String PeekValues = decimalFormat.format(PeekValue);
                            //平均价
                            powerVo.setRate(rates);
                            //能耗
                            powerVo.setNum(numStr);
                            //成本
                            powerVo.setPrice(prices);
                            powerVo.setRateType(rateType);
                            powerVo.setId(id);
                            powerVo.setParentId(parentId);
                            powerVo.setName(name);
                            powerVo.setTime(time);

                            powerVo.setPeekValue(PeekValues);
                            powerVo.setFlatValue(FlatValues);
                            powerVo.setValleyValue(ValleyValues);
                            powerVo.setGraininess(energyDTO.getGraininess());

                            //我这个 listTree 已经是分好组的 数据 就是要遍历 这个 listTree 找出父子级关系 然后 比对时间  如果是父子级id 就比较
                            listTree.add(powerVo);
                        }

                    } else if (energyDTO.getGraininess().equals("4")) {
                        collect = resultList.stream().
                                collect(Collectors.groupingBy(HalfHourPowerMeterSummaryEntity::getMonthStr));
                        for (Map.Entry<String, List<HalfHourPowerMeterSummaryEntity>> entry : collect.entrySet()) {

                            List<HalfHourPowerMeterSummaryEntity> value = entry.getValue();
                            //树的根节点  第一层的所有时间段数据  一个时间段就代表一个树的根节点
                            HashMap<String, Object> mapWyf = new HashMap<>();
                            HashMap<String, Object> mapCxk = new HashMap<>();

                            HashMap<String, Object> mapLh = new HashMap<>();
                            HashMap<String, Object> mapYy = new HashMap<>();

                            //峰
                            HashMap<String, Object> mapPeekValue = new HashMap<>();
                            HashMap<String, Object> mapPeekValues = new HashMap<>();
                            //平
                            HashMap<String, Object> mapFlatValue = new HashMap<>();
                            HashMap<String, Object> mapFlatValues = new HashMap<>();
                            //谷
                            HashMap<String, Object> mapValleyValue = new HashMap<>();
                            HashMap<String, Object> mapValleyValues = new HashMap<>();

                            PowerVo powerVo = new PowerVo();
                            String time = "";
                            String rateType = "";

                            //把设备id 对应的能耗放进map  把时间和类型放到对象里
                            for (HalfHourPowerMeterSummaryEntity hs : value) {
                                //费用
                                mapLh.put(hs.getMeterId(), hs.getTotalPrice());
                                //能耗
                                mapWyf.put(hs.getMeterId(), hs.getDetActiveEnergy());
                                //峰
                                mapPeekValue.put(hs.getMeterId(), hs.getPeekValue());
                                //平
                                mapFlatValue.put(hs.getMeterId(), hs.getFlatValue());
                                //谷
                                mapValleyValue.put(hs.getMeterId(), hs.getValleyValue());
                                time = hs.getMonthStr();
                                rateType = hs.getRateType();
//                                hashSet.add(hs.getMonthStr());

                            }
                            //把设备id拼接ll 对应能耗放进对应id里 有设备id的 没有能耗的放0
                            //[DHU0011,DHU0012]
                            //map{"DHU0012","123321"}
                            for (Object ls : list) {
                                for (Map.Entry<String, Object> stringObjectEntry : mapWyf.entrySet()) {
                                    if (!ls.equals(stringObjectEntry.getKey())) {
                                        mapCxk.put("LL" + ls, 0);

                                    }
                                    if (ls.equals(stringObjectEntry.getKey())) {
                                        mapCxk.put("LL" + ls, stringObjectEntry.getValue());
                                        break;
                                    }
                                }
                            }

                            //把设备id拼接ll 对应费率放进对应id里 有设备id的 没有费率的放0
                            //[DHU0011,DHU0012]
                            //map{"DHU0012","123321"}
                            for (Object ls : list) {
                                for (Map.Entry<String, Object> stringObjectEntry : mapLh.entrySet()) {
                                    if (!ls.equals(stringObjectEntry.getKey())) {
                                        mapYy.put("LL" + ls, 0);

                                    }
                                    if (ls.equals(stringObjectEntry.getKey())) {
                                        mapYy.put("LL" + ls, stringObjectEntry.getValue());
                                        break;
                                    }
                                }
                            }

                            //把设备id拼接ll 对应费率放进对应id里 有设备id的 没有费率的放0
                            //[DHU0011,DHU0012]
                            //map{"DHU0012","123321"}
                            //峰
                            for (Object ls : list) {
                                for (Map.Entry<String, Object> stringObjectEntry : mapPeekValue.entrySet()) {
                                    if (!ls.equals(stringObjectEntry.getKey())) {
                                        mapPeekValues.put("LL" + ls, 0);

                                    }
                                    if (ls.equals(stringObjectEntry.getKey())) {
                                        mapPeekValues.put("LL" + ls, stringObjectEntry.getValue());
                                        break;
                                    }
                                }
                            }
                            //把设备id拼接ll 对应费率放进对应id里 有设备id的 没有费率的放0
                            //[DHU0011,DHU0012]
                            //map{"DHU0012","123321"}
                            //平
                            for (Object ls : list) {
                                for (Map.Entry<String, Object> stringObjectEntry : mapFlatValue.entrySet()) {
                                    if (!ls.equals(stringObjectEntry.getKey())) {
                                        mapFlatValues.put("LL" + ls, 0);

                                    }
                                    if (ls.equals(stringObjectEntry.getKey())) {
                                        mapFlatValues.put("LL" + ls, stringObjectEntry.getValue());
                                        break;
                                    }
                                }
                            }

                            //把设备id拼接ll 对应费率放进对应id里 有设备id的 没有费率的放0
                            //[DHU0011,DHU0012]
                            //map{"DHU0012","123321"}
                            //谷
                            for (Object ls : list) {
                                for (Map.Entry<String, Object> stringObjectEntry : mapValleyValue.entrySet()) {
                                    if (!ls.equals(stringObjectEntry.getKey())) {
                                        mapValleyValues.put("LL" + ls, 0);

                                    }
                                    if (ls.equals(stringObjectEntry.getKey())) {
                                        mapValleyValues.put("LL" + ls, stringObjectEntry.getValue());
                                        break;
                                    }
                                }
                            }


                            //{"llDHU0012+llDHU0011",{LLDHU0012:123},{LLDHU0011:0}}
                            //把上面的公式 和 map放进方法 算出能耗值
                            Float num = Float.parseFloat(ExpressionUtil.eval(s, mapCxk).toString());
                            //费率
                            Float price = Float.parseFloat(ExpressionUtil.eval(s, mapYy).toString());

                            //峰
                            Float PeekValue = Float.parseFloat(ExpressionUtil.eval(s, mapPeekValues).toString());
                            //平
                            Float FlatValue = Float.parseFloat(ExpressionUtil.eval(s, mapFlatValues).toString());
                            //谷
                            Float ValleyValue = Float.parseFloat(ExpressionUtil.eval(s, mapValleyValues).toString());

                            //平均价
                            Float rate = 0.00F;
                            if (num > 0 && price > 0) {
                                rate = (price / num);
                            }
                            //保留小数点两位，采用截断模式
                            num = new BigDecimal(num).setScale(2, RoundingMode.DOWN).floatValue();
                            PeekValue = new BigDecimal(PeekValue).setScale(2, RoundingMode.DOWN).floatValue();
                            FlatValue = new BigDecimal(FlatValue).setScale(2, RoundingMode.DOWN).floatValue();
                            ValleyValue = new BigDecimal(ValleyValue).setScale(2, RoundingMode.DOWN).floatValue();
                            price = new BigDecimal(price).setScale(2, RoundingMode.DOWN).floatValue();
                            rate = new BigDecimal(rate).setScale(2, RoundingMode.DOWN).floatValue();
                            //小数点不足两位添0
                            String numStr = decimalFormat.format(num);
                            String rates = decimalFormat.format(rate);
                            String prices = decimalFormat.format(price);
                            String ValleyValues = decimalFormat.format(ValleyValue);
                            String FlatValues = decimalFormat.format(FlatValue);
                            String PeekValues = decimalFormat.format(PeekValue);
                            //把所有需要的值全放入powerVo这个对象

                            powerVo.setRate(rates);
                            powerVo.setNum(numStr);
                            powerVo.setPrice(prices);
                            powerVo.setRateType(rateType);
                            powerVo.setId(id);
                            powerVo.setParentId(parentId);
                            powerVo.setName(name);
                            powerVo.setTime(time);

                            powerVo.setPeekValue(PeekValues);
                            powerVo.setFlatValue(FlatValues);
                            powerVo.setValleyValue(ValleyValues);
                            powerVo.setGraininess(energyDTO.getGraininess());
                            //我这个 listTree 已经是分好组的 数据 就是要遍历 这个 listTree 找出父子级关系 然后 比对时间  如果是父子级id 就比较
                            listTree.add(powerVo);
                        }

                    } else if (energyDTO.getGraininess().equals("5")) {
                        collect = resultList.stream().
                                collect(Collectors.groupingBy(HalfHourPowerMeterSummaryEntity::getYearStr));

                        for (Map.Entry<String, List<HalfHourPowerMeterSummaryEntity>> entry : collect.entrySet()) {

                            List<HalfHourPowerMeterSummaryEntity> value = entry.getValue();
                            //树的根节点  第一层的所有时间段数据  一个时间段就代表一个树的根节点
                            HashMap<String, Object> mapWyf = new HashMap<>();
                            HashMap<String, Object> mapCxk = new HashMap<>();

                            HashMap<String, Object> mapLh = new HashMap<>();
                            HashMap<String, Object> mapYy = new HashMap<>();

                            //峰
                            HashMap<String, Object> mapPeekValue = new HashMap<>();
                            HashMap<String, Object> mapPeekValues = new HashMap<>();
                            //平
                            HashMap<String, Object> mapFlatValue = new HashMap<>();
                            HashMap<String, Object> mapFlatValues = new HashMap<>();
                            //谷
                            HashMap<String, Object> mapValleyValue = new HashMap<>();
                            HashMap<String, Object> mapValleyValues = new HashMap<>();
                            PowerVo powerVo = new PowerVo();
                            String time = "";
                            String rateType = "";

                            //把设备id 对应的能耗放进map  把时间和类型放到对象里
                            for (HalfHourPowerMeterSummaryEntity hs : value) {
                                //费用
                                mapLh.put(hs.getMeterId(), hs.getTotalPrice());
                                //能耗
                                mapWyf.put(hs.getMeterId(), hs.getDetActiveEnergy());
                                //峰
                                mapPeekValue.put(hs.getMeterId(), hs.getPeekValue());
                                //平
                                mapFlatValue.put(hs.getMeterId(), hs.getFlatValue());
                                //谷
                                mapValleyValue.put(hs.getMeterId(), hs.getValleyValue());
                                time = hs.getYearStr();
                                rateType = hs.getRateType();
//                                hashSet.add(hs.getYearStr());

                            }
                            //把设备id拼接ll 对应能耗放进对应id里 有设备id的 没有能耗的放0
                            //[DHU0011,DHU0012]
                            //map{"DHU0012","123321"}
                            for (Object ls : list) {
                                for (Map.Entry<String, Object> stringObjectEntry : mapWyf.entrySet()) {
                                    if (!ls.equals(stringObjectEntry.getKey())) {
                                        mapCxk.put("LL" + ls, 0);

                                    }
                                    if (ls.equals(stringObjectEntry.getKey())) {
                                        mapCxk.put("LL" + ls, stringObjectEntry.getValue());
                                        break;
                                    }
                                }
                            }

                            //把设备id拼接ll 对应费率放进对应id里 有设备id的 没有费率的放0
                            //[DHU0011,DHU0012]
                            //map{"DHU0012","123321"}
                            for (Object ls : list) {
                                for (Map.Entry<String, Object> stringObjectEntry : mapLh.entrySet()) {
                                    if (!ls.equals(stringObjectEntry.getKey())) {
                                        mapYy.put("LL" + ls, 0);

                                    }
                                    if (ls.equals(stringObjectEntry.getKey())) {
                                        mapYy.put("LL" + ls, stringObjectEntry.getValue());
                                        break;
                                    }
                                }
                            }

                            //把设备id拼接ll 对应费率放进对应id里 有设备id的 没有费率的放0
                            //[DHU0011,DHU0012]
                            //map{"DHU0012","123321"}
                            //峰
                            for (Object ls : list) {
                                for (Map.Entry<String, Object> stringObjectEntry : mapPeekValue.entrySet()) {
                                    if (!ls.equals(stringObjectEntry.getKey())) {
                                        mapPeekValues.put("LL" + ls, 0);

                                    }
                                    if (ls.equals(stringObjectEntry.getKey())) {
                                        mapPeekValues.put("LL" + ls, stringObjectEntry.getValue());
                                        break;
                                    }
                                }
                            }
                            //把设备id拼接ll 对应费率放进对应id里 有设备id的 没有费率的放0
                            //[DHU0011,DHU0012]
                            //map{"DHU0012","123321"}
                            //平
                            for (Object ls : list) {
                                for (Map.Entry<String, Object> stringObjectEntry : mapFlatValue.entrySet()) {
                                    if (!ls.equals(stringObjectEntry.getKey())) {
                                        mapFlatValues.put("LL" + ls, 0);

                                    }
                                    if (ls.equals(stringObjectEntry.getKey())) {
                                        mapFlatValues.put("LL" + ls, stringObjectEntry.getValue());
                                        break;
                                    }
                                }
                            }

                            //把设备id拼接ll 对应费率放进对应id里 有设备id的 没有费率的放0
                            //[DHU0011,DHU0012]
                            //map{"DHU0012","123321"}
                            //谷
                            for (Object ls : list) {
                                for (Map.Entry<String, Object> stringObjectEntry : mapValleyValue.entrySet()) {
                                    if (!ls.equals(stringObjectEntry.getKey())) {
                                        mapValleyValues.put("LL" + ls, 0);

                                    }
                                    if (ls.equals(stringObjectEntry.getKey())) {
                                        mapValleyValues.put("LL" + ls, stringObjectEntry.getValue());
                                        break;
                                    }
                                }
                            }


                            //{"llDHU0012+llDHU0011",{LLDHU0012:123},{LLDHU0011:0}}
                            //把上面的公式 和 map放进方法 算出能耗值
                            Float num = Float.parseFloat(ExpressionUtil.eval(s, mapCxk).toString());
                            //费率
                            Float price = Float.parseFloat(ExpressionUtil.eval(s, mapYy).toString());

                            //峰
                            Float PeekValue = Float.parseFloat(ExpressionUtil.eval(s, mapPeekValues).toString());
                            //平
                            Float FlatValue = Float.parseFloat(ExpressionUtil.eval(s, mapFlatValues).toString());
                            //谷
                            Float ValleyValue = Float.parseFloat(ExpressionUtil.eval(s, mapValleyValues).toString());
                            //平均价
                            Float rate = 0.00F;
                            if (num > 0 && price > 0) {
                                rate = (price / num);
                            }
                            //保留小数点两位，采用截断模式
                            num = new BigDecimal(num).setScale(2, RoundingMode.DOWN).floatValue();
                            PeekValue = new BigDecimal(PeekValue).setScale(2, RoundingMode.DOWN).floatValue();
                            FlatValue = new BigDecimal(FlatValue).setScale(2, RoundingMode.DOWN).floatValue();
                            ValleyValue = new BigDecimal(ValleyValue).setScale(2, RoundingMode.DOWN).floatValue();
                            price = new BigDecimal(price).setScale(2, RoundingMode.DOWN).floatValue();
                            rate = new BigDecimal(rate).setScale(2, RoundingMode.DOWN).floatValue();
                            //小数点不足两位添0
                            String numStr = decimalFormat.format(num);
                            String rates = decimalFormat.format(rate);
                            String prices = decimalFormat.format(price);
                            String ValleyValues = decimalFormat.format(ValleyValue);
                            String FlatValues = decimalFormat.format(FlatValue);
                            String PeekValues = decimalFormat.format(PeekValue);
                            //把所有需要的值全放入powerVo这个对象

                            powerVo.setRate(rates);
                            powerVo.setNum(numStr);
                            powerVo.setPrice(prices);
                            powerVo.setRateType(rateType);
                            powerVo.setId(id);
                            powerVo.setParentId(parentId);
                            powerVo.setName(name);
                            powerVo.setTime(time);
                            powerVo.setPeekValue(PeekValues);
                            powerVo.setFlatValue(FlatValues);
                            powerVo.setValleyValue(ValleyValues);
                            powerVo.setGraininess(energyDTO.getGraininess());
                            //我这个 listTree 已经是分好组的 数据 就是要遍历 这个 listTree 找出父子级关系 然后 比对时间  如果是父子级id 就比较
                            listTree.add(powerVo);
                        }

                    }
                } else {
                    PowerVo powerVo = new PowerVo();
                    //把判断颗粒度时没有 计算公式的放到这个对象集合里
                    powerVo.setId(id);
                    powerVo.setParentId(parentId);
                    powerVo.setName(name);
                    powerVo.setGraininess(energyDTO.getGraininess());

                    powerVo.setPrice("0.00");
                    powerVo.setNum("0.00");
                    powerVo.setRateType("电");
                    //我这个 listTree 已经是分好组的 数据 就是要遍历 这个 listTree 找出父子级关系 然后 比对时间  如果是父子级id 就比较
                    mapPower.put(k, powerVo);
                    //我这个 listTree 已经是分好组的 数据 就是要遍历 这个 listTree 找出父子级关系 然后 比对时间  如果是父子级id 就比较
//                    listPower.add(powerVo);
                }
                if (mapPower.size() > 0) {
                    for (int key : mapPower.keySet()) {
                        PowerVo powerVo = mapPower.get(key);
                        //拿到时间给空值赋值
                        if (energyDTO.getGraininess().equals("2")) {
                            for (String setTime : hashSet) {
                                PowerVo powerVo1 = new PowerVo();
                                powerVo1.setId(powerVo.getId());
                                powerVo1.setParentId(powerVo.getParentId());
                                powerVo1.setGraininess(powerVo.getGraininess());
                                powerVo1.setRateType("电");
                                powerVo1.setPrice("0.00(暂无计算公式)");
                                powerVo1.setNum("0.00(暂无计算公式)");
                                powerVo1.setName(powerVo.getName());
                                powerVo1.setTime(setTime);
                                listTree.add(powerVo1);
                            }
                        }else if(energyDTO.getGraininess().equals("3")){
                            for (String setTime : hashSet1) {
                                PowerVo powerVo1 = new PowerVo();
                                powerVo1.setId(powerVo.getId());
                                powerVo1.setParentId(powerVo.getParentId());
                                powerVo1.setGraininess(powerVo.getGraininess());
                                powerVo1.setRateType("电");
                                powerVo1.setPrice("0.00(暂无计算公式)");
                                powerVo1.setNum("0.00(暂无计算公式)");
                                powerVo1.setName(powerVo.getName());
                                powerVo1.setTime(setTime);
                                listTree.add(powerVo1);
                            }
                        }else if(energyDTO.getGraininess().equals("4")){
                            for (String setTime : hashSet2) {
                                PowerVo powerVo1 = new PowerVo();
                                powerVo1.setId(powerVo.getId());
                                powerVo1.setParentId(powerVo.getParentId());
                                powerVo1.setGraininess(powerVo.getGraininess());
                                powerVo1.setRateType("电");
                                powerVo1.setPrice("0.00(暂无计算公式)");
                                powerVo1.setNum("0.00(暂无计算公式)");
                                powerVo1.setName(powerVo.getName());
                                powerVo1.setTime(setTime);
                                listTree.add(powerVo1);
                            }
                        }else if(energyDTO.getGraininess().equals("5")){
                            for (String setTime : hashSet3) {
                                PowerVo powerVo1 = new PowerVo();
                                powerVo1.setId(powerVo.getId());
                                powerVo1.setParentId(powerVo.getParentId());
                                powerVo1.setGraininess(powerVo.getGraininess());
                                powerVo1.setRateType("电");
                                powerVo1.setPrice("0.00(暂无计算公式)");
                                powerVo1.setNum("0.00(暂无计算公式)");
                                powerVo1.setName(powerVo.getName());
                                powerVo1.setTime(setTime);
                                listTree.add(powerVo1);
                            }
                        }
                    }
                    mapPower.clear();
                }
                k++;
            }
            listTree.forEach(powerVo -> {
                if (powerVo.getId().equals(energyDTO.getAnalysisObject()) || powerVo.getParentId().equals(energyDTO.getAnalysisObject())) {
                    listExcel.add(powerVo);
                }
            });
            //把 listTree 按世间分组
            collectPower = listExcel.stream().collect(Collectors.groupingBy(PowerVo::getTime));
            collectPower.entrySet().stream().sorted(Comparator.comparing(Map.Entry::getKey))
                    .forEach(e -> {
                        Map<String, Object> map = new HashMap<>();
                        map.put(e.getKey(), e.getValue());
                        List<PowerVo> powerVos = e.getValue().stream().map(item -> {
                            PowerVo powerVo = new PowerVo();
                            BeanUtils.copyProperties(item, powerVo);
                            return powerVo;
                        }).collect(Collectors.toList());
                        powerVoList.addAll(powerVos);
                    });

        }
        Collections.sort(powerVoList, Comparator.comparing(PowerVo::getTime).reversed());
        return powerVoList;
    }

    /**
     * 获取有数据得最末时间
     * @param energyDTO
     * @return
     */
    public String getFirstDate(EnergyDTO energyDTO){
        String offsetEndTime = powerMeterSummaryMapper.queryFirstDate(energyDTO);
        offsetEndTime = offsetEndTime == null ? energyDTO.getStartTime() : offsetEndTime;
        return offsetEndTime;
    }
}

